您现在的位置是:首页 > 个人日记个人日记
spring-10-整合mybatis-DAO
2019-02-03 18:51:35【个人日记】345人已围观
简介使用DAO的方式整合mybatis和spring
整合mybatis-DAO
这次jar包较多
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.huangxin</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--log记录-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- mybatis.jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- junit.jar -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring框架导入-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<!--spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.17.RELEASE</version>
<scope>test</scope>
</dependency>
<!--AspectJ AOP环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--spring和mybatis整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--spring-jdbc依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
</dependencies>
</project>
这时候直接使用druid连接池,并且交给spring管理
配置spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描注解-->
<context:component-scan base-package="com.huangxin.order"></context:component-scan>
<!--AOP注解生效-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--加载配置文件-->
<context:property-placeholder location="property/jdbc.properties"></context:property-placeholder>
<!--druid连接池-->
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--SqlSessionFactory工厂交给spring-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--将数据源注入SqlSessionFactory-->
<property name="dataSource" ref="dateSource"></property>
<!--引入映射-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--sqlSession交给spring管理-->
<!--使用多例,因为有多个会话-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
因为在以上配置中已经将对象映射文件引入,所以已经不再使用mybatis-config.xml
文件,可以将其删除
使用DAO方式实现映射
package com.huangxin.order.mapper.impl;
import com.huangxin.order.mapper.OrderMapper;
import com.huangxin.order.model.Orders;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component//将其注册为bean
public class OrderMapperImpl implements OrderMapper {
@Autowired//为其注入
private SqlSession sqlSession;
public Orders getById(Integer id) {
Orders orders = sqlSession.selectOne("com.huangxin.order.mapper.OrderMapper.getById", id);
//spring已经管理,不能调用
// sqlSession.close();
return orders;
}
}
这是对OrderMapper文件的实现,使用注解方式给对象注入
在Junit中直接调用即可
package com.huangxin.order.mapper.impl;
import com.huangxin.order.mapper.OrderMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")//读取配置文件
public class OrderMapperImplTest {
@Autowired
private OrderMapper orderMapper;
@Test
public void getById() {
System.out.println(orderMapper.getById(1));
}
}
这是整合以后,以后开发方式改变
特点
spring已经管理整个SqlSession会话,以及工厂,并且自动提交事务,自动关闭会话,以后只需要写逻辑代码
Tags: JavaWeb
上一篇: 创建博客心得
下一篇: spring-02-配置细节
相关文章
随机图文
评论区
2024-12-21 20:15:11
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信