關於spring整合mybatis的工程配置,已經在Spring+MyBatis實踐—工程配置中所有詳細列出。在此,記錄一下幾種經過MyBatis訪問數據庫的方式。html
一、在spring框架中使用mybatis來進行數據操做配置,參考Spring+MyBatis實踐—工程配置的spring-datasources.xml文件。spring
二、Demo1—經過sqlSessionTemplate來進行數據庫訪問:sql
首先,定義一實體類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:apache
<?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>
【注:注意映射文件開始處指定的命名空間。】mybatis
此時,能夠經過sqlSessionTemplate來進行數據訪問了。app
@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的第二個進行傳入。】框架
經過上述方式來訪問數據庫,比較繁瑣,不直觀。此時,能夠採用另一種方式來調用映射文件User.xml中的數據庫操做。spa
接着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"進行聲明。
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 }