Spring+MyBatis实践—MyBatis数据库访问

关于spring整合mybatis的工程配置,已经在Spring+MyBatis实践—工程配置中全部详细列出。在此,记录一下几种通过MyBatis访问数据库的方式。

1、在spring框架中使用mybatis来进行数据操作配置,参考Spring+MyBatis实践—工程配置的spring-datasources.xml文件。

2、Demo1—通过sqlSessionTemplate来进行数据库访问:

首先,定义一实体类User,并且在数据库(MySQL)中建立了相应的数据表。

 1 public class User {
 2     private int userId;
 3     private String name;
 4     private String pwd;
 5     private String email;
 6     private String address;
 7     private String signature;
 8     private String phone;
 9         
10         /*构造器和getter、setter方法*/
11 }

接着,编写对数据库中表tb_user的映射文件User.xml:

<?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="com.crazysnail.dao.UserDao">
    <select id="getUserByUserId" resultType="User" parameterType="int">
        select * 
        from tb_user 
        where userid=#{userId}
    </select>
</mapper>

【注:注意映射文件开始处指定的命名空间。】

此时,可以通过sqlSessionTemplate来进行数据访问了。

@Service
public class UserService {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    
    public User getUserByUserId(int userId){
        User user = (User)sqlSessionTemplate.selectOne("com.crazysnail.dao.UserDao.getUserByUserId", 1);
        
        return user;
    }
}

【注:sqlSessionTemplate的selectOne方法中的com.crazysnail.dao.UserDao.getUserByUserId要对应到映射文件中的select语句的id,参数通过selectOne的第二个进行传入。】

3、Demo2—通过Dao接口来进行数据库访问。

通过上述方式来访问数据库,比较繁琐,不直观。此时,可以采用另外一种方式来调用映射文件User.xml中的数据库操作。

接着Demo1所做的工作,继续编写UserDao接口。

1 package com.crazysnail.dao;
2 
3 import com.crazysnail.domain.User;
4 
5 public interface UserDao {
6     public User getUserByUserId(int userId);
7     
8 }

在spring的配置文件中添加如下配置(该配置已经在Spring+MyBatis实践—工程配置的spring-datasources.xml文件中添加):

<!-- 用于将接口映射为具体的实例 ,使得在Service中可以直接注入相关的Dao接口来进行数据访问-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:sqlSessionFactory-ref="sqlSessionFactory"
        p:basePackage="com.crazysnail.dao"
        />

该bean用于将Dao接口映射到对应的映射文件,其中映射文件的命名空间对应Dao接口的全称。这就使得UserDao接口同User.xml文件关联起来。

【说明:关联关系的建立,是通过在User.xml文件定义时指定的命名空间名称。User.xml的命名空间定义为com.crazysnail.dao.UserDao,正是UserDao接口的全称。同时,需要注意,在UserDao接口中声明的方法名要对应到User.xml中定义的数据访问过程的id,接口中方法的形参类型对应到User.xml中数据访问过程的parameterType,方法的形参名对应到User.xml中数据访问过程中sql语句中的参数,接口方法的返回值类型对应User.xml中的resultType声明的类型。

如UserDao接口中的方法,

1 public User getUserByUserId(int userId);

对应User.xml中的

<select id="getUserByUserId" resultType="User" parameterType="int">
        select * 
        from tb_user 
        where userid=#{userId}
</select>

说明结束。】 

最后,就可以在service中通过注入UserDao,调用UserDao中声明的接口来进行数据处理了。

1 @Service
2 public class UserService {
3     @Autowired
4     private UserDao userDao;
5     
6     public User getUser(int id){
7         return userDao.getUserByUserId(id);
8     }
9 }

注意:使得MyBatis的数据访问的Xml生效,需要在配置SqlSessionFactoryBean时,通过p:mapperLocations="classpath:com/crazysnail/domain/mybatisConfig/*.xml"进行声明。

4、Demo3—省略映射文件,使用注解:

MyBatis的org.apache.ibatis.annotations包中提供的关于数据访问的注解包括@Select、@Update、@Delete、@Insert,可以用来直接注入简单的SQL语句,省略映射文件。也可映射文件、注解两者结合使用。

单独使用注解时,可以省略掉

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSource"
        p:configLocation="classpath:mybatisConfig.xml"
        p:mapperLocations="classpath:com/crazysnail/domain/mybatisConfig/*.xml" 
        />

中的p:mapperLocations="classpath:com/crazysnail/domain/mybatisConfig/*.xml" 关于映射文件位置的配置。

实例,改写Demo2中的Dao接口,并将映射文件的相关配置去掉,Service层中的代码不改变即可。

1  package com.crazysnail.dao;
2  
3  import com.crazysnail.domain.User;
4  
5  public interface UserDao {
6     @Select("select * from tb_user where userid=#{userId}")
7     public User getUserByUserId(int userId);
8     
9  }

 

posted @ 2014-09-01 16:52  _crazysnail_  阅读(960)  评论(0编辑  收藏  举报