`

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

阅读更多

    在http://dufengx201406163237.iteye.com/blog/2102054中描述了工程的配置,在此记录一下如何使用MyBatis访问数据库;

1、主要配置为:

 

	<!-- 其中p:mapperLocations指定数据库操作文件的地址 -->
	<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" 
		/>
	
	<!-- mybatis数据访问的核心模板 -->
	<bean class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory" />
	</bean>

 

    在Spring的配置文件中配置了sqlSessionTemplate,你可以通过在Service文件中注入sqlSessionTemplate来访问数据库,就像通过Spring为JDBC提供的jdbcTemplate来访问数据库一样。

 

    例如,定义了一User实体类,并且在数据库中设计了相应的数据表;

 

public class User {
	private int userId;
	private String name;
	private String pwd;
	private String email;
	private String address;
	private String signature;
	private String phone;
        
        /*构造器和getter、setter方法*/
}

 

    接下来编写对数据库中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>
	<select id="countUser" resultType="int" parameterType="User">
		select count(*)
		from tb_user
		where email=#{email} and pwd=#{pwd}
	</select>
	<update id="updateUserInfo" parameterType="User">
		update tb_user t
		set t.pwd=#{pwd}, t.name=#{name}
		where t.userid={#userId}
	</update>
	<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="userid">
		insert into tb_user(email,name, pwd)
		values(#{email}, #{name},#{pwd})
	</insert>
	<select id="getUserId" parameterType="String" resultType="int">
		select t.userid
		from tb_user t
		where t.email = #{email}
	</select>
	<select id="getUserByEmail" parameterType="String" resultType="User">
		select *
		from tb_user
		where email = #{email}
	</select>
</mapper>

    其中需要注意的是,User.xml文件开始处指定的命名空间;

 

    此时,你可以通过在service文件中通过注入sqlSessionTemplate,调用User.xml文件中通过select、update、delete、insert定义的数据访问的过程;

 

@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 Bean提供的接口来进行数据访问时,接口参数需要对应到User.xml定义时指定的命名空间和数据访问的id。

 

 

 

2、通过接口来调用映射文件中声明的数据访问过程;

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

     首先声明接口UserDao.java;

 

package com.crazysnail.dao;

import com.crazysnail.domain.User;

public interface UserDao {
	public int countUser(User user);
	public User getUserByUserId(int userId);
	public void updateUserInfo(User user);
	public void addUser(User user);
	public int getUserId(String email);
	public User getUserByEmail(String email);
}

 

   接着,在Spring的配置文件中添加如下配置;

 

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

    通过配置MapperScannerConfigurer,可以将定义的接口文件与对应的数据库操作文件关联起来,如将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接口中的方法,

 

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中声明的接口来进行数据处理;

@Service
public class UserService {
	@Autowired
	private UserDao userDao;
	
	public User getUser(int id){
		return userDao.getUserByUserId(id);
	}
}

 

总结:

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

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics