Spring整合mybatis
一、创建一个java工程;
二、导入jar包(32个);
1、 spring的jar包
2、Mybatis的jar包
3、Spring+mybatis的整合包。
4、Mysql的数据库驱动jar包。
5、数据库连接池的jar包。
三、编写Spring的配置文件
1、数据库连接及连接池
db.properties配置文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
在applicationContext.xml文件中:
<?xmlversion="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--加载数据库连接的配置文件 -->
<context:property-placeholderlocation="classpath:db.properties"/>
<!--数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<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="10"/>
<property name="maxIdle" value="5"/>
</bean>
</beans>
2、事务管理(暂时可以不配置)
3、sqlsessionFactory对象,配置到spring容器中
<!--mapper配置 -->
<!-- 让spring管理sqlsessionFactory,使用mybatis-spring整合包中的SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<propertyname="dataSource" ref="dataSource" />
<!-- 加载mybatis的配置文件-->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
4、mapeer代理对象或者是dao实现类配置到spring容器中。
<!-- 配置mapper扫描包 ······推荐使用 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--value:mapper接口所在的包的全路径 -->
<propertyname="basePackage" value="com.itcast.mybatis.mapper" />
</bean>
四:mapper文件
package com.itcast.mybatis.mapper;
import java.util.List;
import com.itcast.mybatis.po.User;
public interface UserMapper {
public User findUserById(int id ) throws Exception;
public List<User> findUserByUsername(String username) throws Exception;
public void insertUser(User user) throws Exception;
public void updateUser(User user) throws Exception;
public void deleteUser(int id ) throws Exception;
}
五、测试
package com.itcast.mybatis.test;
importjava.util.List;
importorg.junit.Before;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.itcast.mybatis.mapper.UserMapper;
importcom.itcast.mybatis.po.User;
publicclass UserMapperTest {
private ApplicationContextapplicationContext;
//初始化applicationContext
@Before
public void init() throws Exception{
applicationContext= new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
//使用包扫描实现模糊查询
@Test
public voidtestFindUserByUsername() throws Exception {
UserMapper userMapper =applicationContext.getBean(UserMapper.class);
List<User> list =userMapper.findUserByUsername("%张%");
for (User user : list) {
System.out.println(user);
}
}
}