前言 : 在之前DAO实现中使用SqlSessionTemplate的方法,都是采用字符串来指定映射项,这种方式比较容易产生错误,如果拼写错误,在编译器无法识别,只能等到运行的时候才能发现。而且如果命名空间发生变化,会导致很多地方需要修改,不易维护。
applicationContext.xml文件
以下配置,可以省略dao层的实现类。
原因:通过配置MapperFactoryBean 即可自动生成dao层接口的实现类。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 获得sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 映射数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 映射mybatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 映射mapper文件 -->
<property name="mapperLocations">
<list>
<value>classpath:com/bdqn/dao/**/*.xml</value>
</list>
</property>
</bean>
<!--使用 (MapperFactoryBean)注入映射器 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.bdqn.dao.user.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--配置业务Bean,别忘了添加dao层的setter方法 -->
<bean id="userService" class="com.bdqn.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean>
</beans>
DAO层接口
package com.bdqn.dao.user;
import java.util.List;
import com.bdqn.pojo.User;
public interface UserMapper {
/**
* 按照用户名和用户角色查找用户列表
* @param user
* @return
*/
List<User> getUsers(User user);
/**
* 添加用户
* @param user
* @return
*/
int addUser(User user);
}
userMapper.xml文件(保存sql语句)
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bdqn.dao.user.UserMapper">
<select id="getUsers" resultType="User" parameterType="User">
SELECT * FROM `smbms_user`
<where>
<if test="userName!=null and userName!='' ">
and userName LIKE CONCAT('%',#{userName},'%')
</if>
<if test="userRole!=null">
AND `userRole` =#{userRole}
</if>
</where>
</select>
<insert id="addUser" parameterType="User">
INSERT INTO `smbms_user` (userCode,userName,userPassword) VALUES (#{userCode},#{userName},#{userPassword})
</insert>
</mapper>
service的接口(UserService)
package com.bdqn.service.user;
import java.util.List;
import com.bdqn.pojo.User;
public interface UserService {
List<User> findUserList(User user) throws Exception;
int addUser(User user);
}
service层的实现类
注意:不要忘记了userMapper属性的getter和setter方法,其实,可以省略getter方法,只是习惯问题。
原因:spirng配置文件中定义UserService的Bean采用的是DI(依赖注入),由于DI采用setter方法传值,因此需要userMapper的getter和setter方法。
package com.bdqn.service.user;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.bdqn.dao.user.UserMapper;
import com.bdqn.pojo.User;
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
/*---------------不要忘了我哦---------------*/
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
/*---------------不要忘了我哦---------------*/
@Override
public List<User> findUserList(User user) throws Exception {
try {
return userMapper.getUsers(user);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
}
测试类
package com.bdqn;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bdqn.pojo.User;
import com.bdqn.service.user.UserService;
public class UserTest {
@Test
public void eg1(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service= (UserService) ctx.getBean("userService");
User user=new User();
user.setUserName("赵");
try {
List< User> list=service.findUserList(user);
for (User u : list) {
System.out.println(u.getUserCode()+"\t"+u.getUserName());
}
} catch (Exception e) {
e.printStackTrace();
}
/*运行结果:
zhaoyan 赵燕
zhaomin 赵敏
zhaosi 赵四*/
}
}