spring和mybatis整合
1.创建一个新的java工程
添加jar包:
mybatis的jar包
spring的jar包
mybatis和spring整合包
创建一个config的包:
mybatis
---SqlMapConfig.xml
spring
---applicationContext.xml
log4j.properties
db.properties
配置applicationContext.xml文件
先配置sqlSessionFactory:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="mybatis/sqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="mybatis/sqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
配置数据源:
<!-- 配置数据源,dbcp -->
<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 ,dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 使用c3p0的数据源进行数据的绑定 -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 ,dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 使用c3p0的数据源进行数据的绑定 -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
<!-- 使用c3p0来进行数据库的链接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="5" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="5" />
</bean>
在SqlMapconfig.xml中加载User.xml
配置SqlMapConfig.xml文件
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
原始dao开发(和spring整合后)
User.xml
<mapper namespace="test">
<!-- 在映射文件中配合很多的sql语句-->
<select id="findUserById" parameterType="int" resultType="cn.itcast.ssm.po.User">
SELECT * FROM USER WHERE id=#{id}
</select>
</mapper>
<!-- 在映射文件中配合很多的sql语句-->
<select id="findUserById" parameterType="int" resultType="cn.itcast.ssm.po.User">
SELECT * FROM USER WHERE id=#{id}
</select>
</mapper>
dao(实现类继承SqlSessionDaoSupport)
public interface UserDao {
//通过用户id查询用户
public User findUserById(int id) throws Exception;
//通过用户id查询用户
public User findUserById(int id) throws Exception;
dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。
这里spring声明配置方式,配置dao的bean:
让UserDaoImpl实现类继承SqlSessionDaoSupport
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("test.findUserById", id);
return user;
}
配置dao
在 applicationContext.xml 中配置 dao 。
<bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
测试:
private ApplicationContext application;
@Before
public void setUp() throws Exception {
application = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception {
userDao us = (userDao) application.getBean("userDao");
User user = us.findUserById(1);
System.out.println(user);
}
@Before
public void setUp() throws Exception {
application = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception {
userDao us = (userDao) application.getBean("userDao");
User user = us.findUserById(1);
System.out.println(user);
}
开发mapper的方法:
先实现mapper.xml和mapper.class的接口
public interface UserMapper {
public User findUserByIdMapper(int id) throws Exception;
}
public User findUserByIdMapper(int id) throws Exception;
}
<mapper namespace="cn.itcast.ssm.mapper.UserMapper">
<select id="findUserByIdMapper" parameterType="int" resultType="cn.itcast.ssm.po.User">
select * from USER where id=#{id}
</select>
</mapper>
<select id="findUserByIdMapper" parameterType="int" resultType="cn.itcast.ssm.po.User">
select * from USER where id=#{id}
</select>
</mapper>
然后在applicationContext.xml中实现接口的实例化:
<bean id="userMapper" class="
org.mybatis.spring.mapper.MapperFactoryBean
">
<property name="mapperInterface" value="cn.itcast.ssm.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<property name="mapperInterface" value="cn.itcast.ssm.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
批处理的方法(比较常用):
<!-- mapper批量扫描,从mapper包中扫描mapper接口,自动创建代理对象并且在spring容器中注 遵循规范:将mapper,java和mapper.xml映射文件名称保持一致,并且在一个目录中自动 扫描出来mapper的bean的id为mapper(首字母小写) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
进行测试:
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void testFindUserByIdMapper() throws Exception {
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
User user = userMapper.findUserByIdMapper(1);
System.out.println(user);
}
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void testFindUserByIdMapper() throws Exception {
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
User user = userMapper.findUserByIdMapper(1);
System.out.println(user);
}