一.jar包准备和配置文件的准备
1.spring 的依赖包
2.mybatis的依赖包
3.MyBatis和Spring框架集成的桥梁包
4.数据库驱动包和连接池
5.Mybatis支持的日志包log4j
6.项目集成需要的各种配置文件
二.service层和mapper层的维护
1.service层
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int insert(User user) {
return userMapper.insert(user);
}
@Override
public int deleteByPrimaryKey(Integer id) {
return userMapper.deleteByPrimaryKey(id);
}
@Override
public int updateByPrimaryKey(User user) {
return userMapper.updateByPrimaryKey(user);
}
@Override
public User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
public List<User> selectList() {
return userMapper.selectList();
}
2.测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testInsert(){
User user = new User(null, "小明", "xm123", 20);
int row = userService.insert(user );
System.out.println("row :"+row);
}
}
3.mapper中的接口和xml文件
public interface UserMapper {
//DML
int insert(User user);
int deleteByPrimaryKey(Integer id);
int updateByPrimaryKey(User user);
//DQL
User selectByPrimaryKey(Integer id);
List<User> selectList();
}
<?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.sxt.mapper.UserMapper">
<insert id="insert" parameterType="com.sxt.bean.User"
keyColumn="id" keyProperty="id" useGeneratedKeys="true"
>
insert into user (name,password,age)values(#{name},#{password},#{age})
</insert>
<!-- 删除功能 -->
<delete id="deleteByPrimaryKey" parameterType="Integer">
delete from user where id = #{id}
</delete>
<!-- 修改功能 -->
<update id="updateByPrimaryKey" parameterType="com.sxt.bean.User">
update user set name = #{name},password=#{password},age=#{age} where id = #{id}
</update>
<select id="selectByPrimaryKey" parameterType="Integer" resultType="com.sxt.bean.User">
select * from user where id = #{id}
</select>
<select id="selectList" resultType="com.sxt.bean.User">
select * from user
</select>
</mapper>
4.applicationContext.xml中的配置(重点)
配置内容:
1)注解的包扫描配置
<context:component-scan base-package="com.sxt"/>
2)读取db.properties文件
<context:property-placeholder location="classpath:db.properties"/>
3)配置dataSource
<bean id="dataSorce" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
4)创建SqlSessionFacotry对象
里面必须配置的属性:
a.配置连接池dataSource
里面可选的属性配置
a.包扫描别名配置:如果在mybatis-config.xml中已配置,可以省略该配置
b.映射文件的读取:userMapper.xml:如果userMapper和其xml文件在同一个包中,可以省略该配置
c.读取mybatis-config.xml:可以在这个xml中保留一些mybatis的配置,例如驼峰命名
<!--做还没集成前的第一步:创建SqlSessionFacotry对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源连接池-->
<property name="dataSource" ref="dataSorce"/>
<!--包扫描别名配置-->
<property name="typeAliasesPackage" value="com.sxt"/>
<!--读取映射文件userMapper.xml[可选],底层会自动绑定映射文件-->
<!--注意要用数组,且用/,不是.-->
<!--如果mapper的接口和mapper放在同一个包下,此配置可以省略-->
<property name="mapperLocations">
<array>
<value>classpath:com/sxt/mapper/*Mapper.xml</value>
</array>
</property>
<!--读取mybatis-config.xml,可保留设置配置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
5)创建UserMapper映射接口的代理对象
里面需要配置的属性
a.配置映射文件的接口
b.配置工厂对象
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--配置映射文件的接口 userMapper-->
<property name="mapperInterface" value="com.sxt.mapper.UserMapper"/>
<!--配置工厂对象-->
<property name="sqlSessionFactoryBeanName" ref="sqlSessionFactory"/>
</bean>
注意:如果有多个mapper,工厂对象建议使用包扫描
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置映射接口的包-->
<property name="basePackage" value="com.sxt.mapper"/>
<!--配置SqlSessionFacotry的名称 [可选]-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>