JAVA框架 Spring 和Mybatis整合(传统dao)

时间:2023-03-10 04:53:43
JAVA框架  Spring 和Mybatis整合(传统dao)

一:我们使用spring处理service,mybaits处理dao层。

二:导入jar包

pom.xml文件内容:

 <?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.</modelVersion> <groupId>jd.com</groupId>
<artifactId>AddSpringAndMy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>AddSpringAndMy Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.</version>
</dependency>
<dependency>
<groupId>spring-jms</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3..RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.1..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3..RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>spring-aspects</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.</version>
</dependency>
<dependency>
<groupId>com.springsource.org</groupId>
<artifactId>aspectj.weaver</artifactId>
<version>1.6.</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0.</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1..RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.16.-GA</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1..RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3..RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies> <build>
<finalName>AddSpringAndMy</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

注意:

在引用jar包的话,会出现jar包冲突,注意版本的引用,最好版本号一致。

jar包清单:

JAVA框架  Spring 和Mybatis整合(传统dao)

三、配置文件整合

1)首先引用properites文件(注意引用外部文件使用classpath引用)

 <context:property-placeholder location="classpath:db.properites" />

2)开启注解扫描和动态代理

     <context:component-scan base-package="jd.com" />
<aop:aspectj-autoproxy />

3)我们将DataSource和sqlSessionFactory叫给spring个管理

     <!--定义数据库连接池-->
<bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" >
<!--支持el表达式-->
<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="maxTotal" value="" />
<property name="maxIdle" value="" />
</bean>

因为SqlSessionFactory是接口,可以使用mybatis-spring的整合的实现类:SqlSessionFactoryBean

JAVA框架  Spring 和Mybatis整合(传统dao)

JAVA框架  Spring 和Mybatis整合(传统dao)

JAVA框架  Spring 和Mybatis整合(传统dao)

JAVA框架  Spring 和Mybatis整合(传统dao)

其中需要在注入DataSource和配置文件的注入。注意需要是用classpath文件引用

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--依赖DataSource-->
<property name="dataSource" ref="basicDataSource"/>
<!--加载mybaits的配置文件-->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>

4)注入dao层和service层

     <bean id="dao" class="jd.com.dao.trDaoImpl">
<property name="SqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean class="jd.com.service.serFindByIdImpl" id="serv">
<property name="dao" ref="dao" />
</bean>

applicationContext.xml完整配置文件:

 <?xml version="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">
<!--加载配置文件,定义的properites文件
引入外部文件使用classpath关键字。
--> <context:component-scan base-package="jd.com" />
<aop:aspectj-autoproxy /> <context:property-placeholder location="classpath:db.properites" />
<!--定义数据库连接池-->
<bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" >
<!--支持el表达式-->
<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="maxTotal" value="" />
<property name="maxIdle" value="" />
</bean>
<!--配置mapper
其中:org.mybatis.spring.SqlSessionFactoryBean 是SqlSessionFactory的实现类,该类在mybatis-spring.1.2..jar里。
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--依赖DataSource-->
<property name="dataSource" ref="basicDataSource"/>
<!--加载mybaits的配置文件-->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>
<bean id="dao" class="jd.com.dao.trDaoImpl">
<property name="SqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean class="jd.com.service.serFindByIdImpl" id="serv">
<property name="dao" ref="dao" />
</bean> </beans>

其中db.properits是配置数据库配置。

在看下mybatis的配置文件:只是一个子配置文件引入:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<mappers >
<mapper resource="user.xml" /> <!--<mapper class="jd.com.proxydao.AccMapper" />-->
<!--<package name="jd.com.proxydao" />-->
<!--<package name="jd.com.vo" />-->
<!--<package name="jd.com.vodemo" />-->
<!--导入配置文件-->
<!--<package name="jd.com" />-->
</mappers>
</configuration>

子配置文件user.xml:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--和主配置文件约束是不一样的-->
<mapper namespace="findById">
<select id="findId" parameterType="int" resultType="jd.com.dao.user" >
SELECT * FROM username WHERE id=#{id};
</select>
</mapper>

注意:在不使用mybatis的动态代理的情况下,需要注意 namespace和id不是对应的接口的接口名和方法。

user.class实体类:

 package jd.com.dao;

 public class user {
private Integer id;
private String name;
private String sex; public void setName(String name) {
this.name = name;
} public void setSex(String sex) {
this.sex = sex;
} public void setId(Integer id) {
this.id = id;
} public Integer getId() {
return id;
} public String getName() {
return name;
} public String getSex() {
return sex;
} @Override
public String toString() {
return this.getName()+" "+this.getId()+" "+this.getSex();
}
}

dao层:

接口:

 package jd.com.dao;

 public interface trDao {
user findUserByName(Integer id);
}

实现类:注意这个里继承扫起来SqlSessionSupport类。在注入dao层的时候需要注意的引用sqlsessionfactory  所以这之前spring的自己daosupport一样需要引用相应的(DataSource)

 package jd.com.dao;

 import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository; //需要继承SqlSessionDaoSupport public class trDaoImpl extends SqlSessionDaoSupport implements trDao { @Override
public user findUserByName(Integer id) {
// SqlSessionDaoSupport该类有相应的set方法和get方法 应该类型spring的DaoSupport
SqlSession sqlSession=this.getSqlSession(); user use=sqlSession.selectOne("findById.findId",id);
return use;
}
}

service层:

接口:

 package jd.com.service;

 import jd.com.dao.user;

 public interface trService {
user serFindById(Integer id);
}

实现类:

 package jd.com.service;

 import jd.com.dao.trDao;
import jd.com.dao.user;
import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service(value = "serv")
public class serFindByIdImpl implements trService {
@Resource(name="dao")
private trDao dao; @Override
public user serFindById(Integer id) {
user use=this.dao.findUserByName(id);
return use;
}
}

测试类:

 package jd.com.testDemo;

 import jd.com.dao.user;
import jd.com.service.serFindByIdImpl;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class testDemo { // @Resource(name="sev")
// private serFindByIdImpl serv; @Test
public void test1(){
ApplicationContext ap=new ClassPathXmlApplicationContext("applicationContext.xml");
serFindByIdImpl serv= (serFindByIdImpl) ap.getBean("serv");
System.out.println();
user us=serv.serFindById();
System.out.println(us); }
}
;