论坛首页 Java企业应用论坛

整合Mybatis与Spring3

浏览 56861 次
精华帖 (1) :: 良好帖 (1) :: 新手帖 (4) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-04-26  
上午参考了一些文章,整合了spring+mybatis,现在共享一下。把代码帖出来,如果有什么疑问,到mybatis官网下载相关文档来看一下就可以了。如果没有基础,请看我之前发的文章。
1.实体bean
package org.hyn.bean;

public class User {
	private int id;
	private String name;
	private int age;
...//长长的set get 方法


2.Mapper及配置文件
package org.hyn.maper;

import org.hyn.bean.User;

public interface UserMapper {
	public void insertUser(User user);

	public User getUser(String name);
}


<?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE mapper 
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.hyn.maper.UserMapper">
	<insert id="insertUser" parameterType="org.hyn.bean.User">
		insert into user(name,age) values(#{name},#{age}) 
        </insert>
	<select id="getUser" resultType="org.hyn.bean.User"
		parameterType="java.lang.String">
		select * from user where name=#{name} 
       </select>

	<!-- 当使用该Mybatis与Spring整合的时候,该文件必须和相应的Mapper接口文件同名,并在同一路径下 -->
</mapper> 

3.spring配置文件
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="myDataSource" />

		<!-- <property name="configLocation" value=""/> -->
		<!--
			该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的
			元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。因为这些都是使用Spring中的配置
			。当然如果你不打算添加typeAlias 之类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了
		-->

		<!--<property name="mapperLocations" value="src/UserMapper.xml"/>-->
		<!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名,且在同一路径下,那么可以不配置该选项-->
	</bean>

	<!--
		注册Mapper方式一 <bean id="userMapper"
		class="org.mybatis.spring.mapper.MapperFactoryBean"> <property
		name="mapperInterface"
		value="org.hyn.maper.UserMapper"/> <property
		name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
	-->

	<!-- 注册Mapper方式二:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper ,配置如下:-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="org.hyn.maper" />
	</bean>

4.测试类
	@Test
	public void testGetUser() {
		ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
		UserMapper userMapper = aContext.getBean(UserMapper.class);
		User user = userMapper.getUser("张三");
		System.out.println(user.toString());
	}
	
	@Test
	public void testAddUser(){
		ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
		UserMapper userMapper = aContext.getBean(UserMapper.class);
		User user = new User();
		user.setName("张三");
		user.setAge(18);
		userMapper.insertUser(user);
		System.out.println("添加成功");
	}


下面付上源代码
   发表时间:2011-04-27  
好例子!!!

请问如何才能得到添加记录"张三"的ID值? 能在插入时直接回填ID吗?
0 请登录后投票
   发表时间:2011-04-27  
liuyiqi2008 写道
好例子!!!

请问如何才能得到添加记录"张三"的ID值? 能在插入时直接回填ID吗?

你说的是用WEB来访问吗?我这只是用junit测试
0 请登录后投票
   发表时间:2011-04-27  
是一样的呀,插入的时候往数据库中增加"张三"这条记录,ID由MYSQL自动加,这个自动加的ID能回填到user.id中吗?不用再查询DB,就像其他持久包一样。
0 请登录后投票
   发表时间:2011-04-27  
liuyiqi2008 写道
是一样的呀,插入的时候往数据库中增加"张三"这条记录,ID由MYSQL自动加,这个自动加的ID能回填到user.id中吗?不用再查询DB,就像其他持久包一样。

可以啊,你测试一下就可以了
0 请登录后投票
   发表时间:2011-04-27  
加了一行user输出:
@Test 
public void testAddUser(){  
    ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");  
    UserMapper userMapper = aContext.getBean(UserMapper.class);  
    User user = new User();  
    user.setName("张三");  
    user.setAge(18);  
    userMapper.insertUser(user);
    System.out.println(user.toString());  
    System.out.println("添加成功");  
}

运行结果:
id:0,name:张三,age:18
添加成功

数据库中这条记录的id为27,因此user.id没有被回填为27,所有问有无方法自动回填,期待的结果是:
id:27,name:张三,age:18
添加成功
0 请登录后投票
   发表时间:2011-04-28  
我用Mybatis+Spring3遇到过灵异事件
0 请登录后投票
   发表时间:2011-04-28  
官方的文档讲的已经蛮清楚了
0 请登录后投票
   发表时间:2011-04-28  
想问下只用接口的时候,分页怎么处理比较好
0 请登录后投票
   发表时间:2011-04-28  
又见整合!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics