spring-mybatis整合
- 掌握spring与mybatis整合方式
- 两类一数据源
SqlSessionFactoryBean
作用:用来创建SqlSessionFacotry,并且扫描xml文件(mybatis中的扫描)
dataSource:数据源
- typeAliasesPackage:扫描某个包,并将下面的类配置别名
- configLocation:加载mybatis主配置文件
- mapperLocations:扫描某个包下所有mapper.xml
- MapperScannerConfigurer
作用:用来扫描mapper接口
basePackage:扫描某个包下所有mapper接口
- sqlSessionFactoryBeanName:注入sqlSessionFactory,注意是用value,不是ref的方式注入
spring-mybatis整合步骤
导包:1、mybatis.jar 2、mybatis-spring.jar 3、spring-jdbc.jar 4、spring-tx.jar 5、c3p0.jar
1、编写Mapper接口
//这里的代理对象由mybatis创建,不能使用@Service
public interface UserDao {
//使用@Param("username")取别名
User selectUser(@Param("username")String username,@Param("password")String password);
}
2、编写Mapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hemi.dao.UserDao">
<select id="selectUser" resultType="User">
select * from user where username=#{username} and password=#{password}
</select>
</mapper>
3、编写service
//不要忘了使用注解创建对象
@Service
public class UserService {
//不要忘了使用注解注入对象
@Autowired
private UserDao userDao;
public User selectUser(String username,String password){
User user = userDao.selectUser(username, password);
return user;
}
}
4、编写spring-mybatis.xml
<!-- 加载配置文件 -->
<context:property-placeholder location="db.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactoryBean:用来创建SqlSessionFacotry,并且扫描xml文件-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFacotry">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置别名,如果在下面的mybatis配置文件中有配置别名,那么这句可以不用-->
<property name="typeAliasesPackage" value="实体类所在的包名"></property>
<!--加载mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 扫描mapper.xml -->
<property name="mapperLocations" value="classpath:com/hemi/mapper/*.xml"></property>
</bean>
<!-- 配置MapperScannerConfigurer:用来扫描mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描mapper接口 -->
<property name="basePackage" value="接口所在的包名"/>
<!--注入sqlSessionFacotry-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property>
</bean>
5、db.properties文件
jdbc.initialPoolSize=10
jdbc.acquireIncrement=5
jdbc.minPoolSize=2
jdbc.maxPoolSize=50
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/app?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=