Spring整合Mybatis
Spring整合Mybatis以后在sqlMapConfig.Xml几乎不用放什么配置文件,都交给了Spring容器进行管理的,数据库的连接,sqlSessionFantory.
- applictionContext.Xml(传统的实现)
<!-- 进行dbcp连接池的配置文件加载 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- dbcp的连接配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<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>
<!-- 在spring中对进行sqlSessionFantory的配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis的核心文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
<!--配置连接池对象 -->
<property name="dataSource" ref="dataSource"/>
<!-- 进行别名的扫描操作 -->
<property name="typeAliasesPackage" value="com.al.Mybatis_spring.pojo"/>
</bean>
<!--进行dao层的传统的额配置 配置dao层到Spring中 -->
<!-- 这个地方sqlSessionFactory属性注入到UserDao
是因为UserDao继承了SqlSessionDaoSupport,在sqlSessionDaoSupporet
中有一个属性sqlSessionFantory,实现了这个属性的话,里面会去实现一个方法叫做
getSqlSession的方法,获取到这个sql操作的对话,这样就可以进行数据库中的操作了
-->
<bean id="UserDao" class="com.al.Mybatis_spring.dao.UserDaoImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
DAO层
public class UserDaoImp extends SqlSessionDaoSupport implements UserDao {
@Override
public User getUserById(Integer id) {
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("user.getUserById", id);
//不要关闭sqlSeesion
//sqlSession.close();
return user;
}
@Override
public List<User> getUserByUserName(String name) {
SqlSession sqlSession = this.getSqlSession();
List<User> selectList = sqlSession.selectList("user.getUserByName_like", name);
//不要关闭sqlSeesion
//sqlSession.close();
return selectList;
}
@Override
public void insertUser(User user) {
SqlSession sqlSession = this.getSqlSession();
sqlSession.insert("user.setUser", user);
//不要关闭sqlSeesion
//sqlSession.close();
}
}
sqlMapConfig.xml
<configuration>
<!-- 整合spring以后里面的东西基本不要了 -->
<mappers>
<!-- 加载传统dao层的映射文件 -->
<mapper resource="Mapper/User.xml"/>
</mappers>
</configuration>
动态代理实现
- 使用动态代理的形式进行配置
<!-- 使用动态代理的形式来进行整合 配置代理类的操作-->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true">
配置sqlSessionFanctory
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
配置mapper的接口
<bean parent="userMapper">
<property name="mapperInterface" value="com.al.Mybatis_spring.Mapper.UserMapper"></property>
</bean> -->
<!-- 使用包扫描的新式进行配置操作(第二种动态代理的实现) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 对扫描包的配置 -->
<property name="basePackage" value="com.al.Mybatis_spring.ssm.Mapper"/>
</bean>
测试持久层 这里使用的逆向工程生成的mapper和pojo
public class Gssm_Test_01 {
private ApplicationContext app;
@Before
public void init() {
this.app=new ClassPathXmlApplicationContext("classpath:applictionContext.xml");
}
//@Test
public void testinsert() {
//获取到代理对象
UserMapper userMapper = this.app.getBean(UserMapper.class);
User user=new User();
user.setUsername("小鬼");
user.setSex("1");
user.setBirthday(new Date());
user.setAddress("湖北武汉");
user.setUuid("11111");
userMapper.insert(user);
}
@Test
public void TestSelectByExample() {
UserMapper userMapper = this.app.getBean(UserMapper.class);
/*这个地方可以使用单表的的多条件的查询操作
* 创建一个Example的对象
*/
UserExample userexample=new UserExample();
userexample.createCriteria().andUsernameLike("%小%");
userexample.createCriteria().andSexEqualTo("1");
//将条件添加到代理类中(进行该条件的查询操作)
List<User> selectByExample = userMapper.selectByExample(userexample);
for (User user : selectByExample) {
System.out.println(user);
}
}
//@Test
public void Testinsert() {
//获取到反射的对象
UserMapper userMapper = this.app.getBean(UserMapper.class);
User user=new User();
user.setUsername("哈哈");
user.setBirthday(new Date());
/*
* insertSelective(这个进行数据保存的时候是会根据 你插入的数据种是否存在该字段
* 进行的sql语句的添加操作,所以的话,这个插入的,跟为的实用方便
* )
*/
userMapper.insertSelective(user);
}
}