1.所需要的包:
1,spring发行包
2,mybatis 包和Mybatis-Spring 的jar包,maven可以使用:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
<scope>compile</scope>
</dependency>
2.配置
spring applicationContent.xml 文件主要内容:
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ---------1
<property name="dataSource" ref="dataSource" /> -------2
<!-- 显式指定Mapper文件位置 -->
<property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml" /> -------3
<property name="typeAliasesPackage" value="com.blueinfo.jee.entity" /> -------4
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> ------5
<property name="basePackage" value="com.blueinfo.jee.reposity.mybatis" /> --------6
<property name="annotationClass" value="com.blueinfo.jee.reposity.mybatis.MyBatisReposity"/> --------7
</bean>
注释:
1,定义SqlSessionFactoryBean,在这个bean里面还是通过SqlSessionFactoryBuilder来建立对应的SqlSessionFactory,进而获取到对应的SqlSession。通过SqlSessionFactoryBean我们可以通过对其指定一些属性来提供Mybatis的一些配置信息。
2,datasource定义,不能缺少
3,显示指定Mapper.xml的位置
4,指定实体类的包位置
5,通过配置MapperScannerConfigurer,Mybatis-Spring会自动为我们注册Mapper对应的MapperFactoryBean对象,而不用使用类似下面的配置一个个指定
<bean id="myMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.blueinfo.jee.BlogMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
上述方式,有一个Mapper就要定义一个bean。
6,指定接口的包,MapperScannerConfigurer会扫面包下面的所有Mapper接口。并把它们注册为一个个MapperFactoryBean对象
7,通过注解过滤基本包下面的接口。使得MapperScannerConfigurer只扫描basePackage下面带注解MyBatisReposity的接口
3.源文件
实体类:
@Entity
@Table(name = "ss_user")
public class User extends IdEntity {
private String loginName;
private String plainPassword;
private String password;
private String salt;
private String name;
private String email;
private String status;
//.....
}
@MappedSuperclass
public abstract class IdEntity {
protected Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
注解MyBatisReposity
/**
* 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描。
*
* @author calvin
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Component
public @interface MyBatisReposity {
String value() default "";
}
dao:
@MyBatisReposity
public interface UserMybatisDao {
User get(Long id);
List<User> search(Map<String, Object> parameters);
void save(User user);
void delete(Long id);
}
service:
@Service
public class AccountService implements IAccoutService{
@Autowired
private UserMybatisDao userMbDao;
public void getUser(long id){
User user = userMbDao.get(id);
logger.info("user name:" + user.getLoginName());
}
Logger logger = LoggerFactory.getLogger(AccountService.class);
}
UserMapper.xml 与dao接口一一对应
<?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">
<!-- namespace必须指向Dao接口 -->
<mapper namespace="com.blueinfo.jee.reposity.mybatis.UserMybatisDao">
<!--
获取用户: 输出直接映射到对象, login_name列要"as loginName"以方便映射 ,team_id as "team.id"创建team对象并赋值
-->
<select id="get" parameterType="long" resultType="User">
select id, name, email,
login_name as loginName,
team_id as "team.id"
from ss_user
where id=#{id}
</select>
</mapper>
test类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
@Transactional
public class AccountServiceTest {
@Autowired
private AccountService service;
@Test
public void testGetUser() {
User user = service.getUser(1L);
assertNotNull("User not found", user);
assertEquals("test", user.getLoginName());
}
}
文章内容参考开源项目SpringSide,地址:https://github.com/springside