一、导入jar包,这里使用Maven项目的导包方式pom.xml
测试类注解依赖
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.11</ version>
< scope> test</ scope>
</ dependency>
数据库依赖
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
Mybatis依赖
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.2</ version>
</ dependency>
Spring-webmvc依赖
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.1.9.RELEASE</ version>
</ dependency>
Spring-jdbc依赖
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.1.9.RELEASE</ version>
</ dependency>
aspectjweaver依赖
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.8.13</ version>
</ dependency>
Spring+Mybatis依赖
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 2.0.2</ version>
</ dependency>
实体类注解依赖(需要安装lombok插件)
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.16.10</ version>
</ dependency>
</ dependencies>
编译文件目录设置
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
</ resources>
</ build>
二、创建实体类Student.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private String sex;
private int age;
}
三、创建数据操作类接口StudentDao.java
public interface StudentDao {
public List < Student > SelectStudent ( ) ;
public int addStudent ( Student stu) ;
public int updateStudent ( Student stu) ;
public int deleteStudent ( Student stu) ;
}
四、创建操作类接口的实现类StudentDaoImpl.java
public void setSqlSession ( SqlSessionTemplate sqlSession) {
this . sqlSession = sqlSession;
}
@Override
public List < Student > SelectStudent ( ) {
return sqlSession. getMapper ( StudentDao . class ) . SelectStudent ( ) ;
}
@Override
public int addStudent ( Student stu) { return 0 ; }
@Override
public int updateStudent ( Student stu) {
return 0 ;
}
@Override
public int deleteStudent ( Student stu) {
return 0 ;
}
}
五、编写数据操作类方法的映射文件Student.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 = " cn.daitools.merge.StudentDao" >
< select id = " SelectStudent" resultType = " cn.daitools.pojo.Student" >
select *
from student
</ select>
< insert id = " addStudent" >
insert into student(name, age, sex)
values (#{name}, #{age}, #{sex})
</ insert>
< delete id = " deleteStudent" >
delete
from student
where name = #{name}
</ delete>
< update id = " updateStudent" >
update student
set name = age = {#age}
where #{name}
</ update>
</ mapper>
六、编写Spring的核心配置文件Spring_Mybatis.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: util= " http://www.springframework.org/schema/util"
xmlns: p= " http://www.springframework.org/schema/p" xmlns: c= " http://www.springframework.org/schema/c"
xmlns: context= " http://www.springframework.org/schema/context"
xmlns: tx= " http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" >
< bean id = " DataSource" class = " org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name = " driverClassName" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/ssm" />
< property name = " username" value = " root" />
< property name = " password" value = " 082800zjh" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " DataSource" />
< property name = " mapperLocations" value = " classpath:cn/daitools/merge/StudentDao.xml" />
</ bean>
< bean id = " sqlSession" class = " org.mybatis.spring.SqlSessionTemplate" >
< constructor-arg name = " sqlSessionFactory" ref = " sqlSessionFactory" />
</ bean>
< bean id = " StudentDaoImpl" class = " cn.daitools.merge.StudentDaoImpl" >
< property name = " sqlSession" ref = " sqlSession" />
</ bean>
< bean id = " StudentDaoImpl2" class = " cn.daitools.merge.StudentDaoImpl2" >
< property name = " sqlSessionFactory" ref = " sqlSessionFactory" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " DataSource" />
</ bean>
< tx: advice id = " txAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " *" propagation = " REQUIRED" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " pointcut" expression = " execution(* cn.daitools.merge.*.*(..))" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " pointcut" />
</ aop: config>
</ beans>
七、编写测试类
public class MargeTest {
@Test
public void test ( ) {
ApplicationContext context = new ClassPathXmlApplicationContext ( "spring_mybatis.xml" ) ;
StudentDao studentDaoImpl = ( StudentDao ) context. getBean ( "StudentDaoImpl2" , StudentDao . class ) ;
List < Student > list = studentDaoImpl. SelectStudent( ) ;
for ( Student student : list) {
System . out. println ( student) ;
}
}
}
八、第四部中获取Sqlsession可以使用另外一种方式
public class StudentDaoImpl2 extends SqlSessionDaoSupport implements StudentDao {
@Override
public List < Student > SelectStudent ( ) {
Student stu = new Student ( ) ;
stu. setSex ( "nv" ) ;
stu. setName ( "呆呆" ) ;
stu. setAge ( 21 ) ;
StudentDao studentDao = getSqlSession ( ) . getMapper ( StudentDao . class ) ;
studentDao. addStudent ( stu) ;
studentDao. deleteStudent ( stu) ;
return studentDao. SelectStudent( ) ;
}
@Override
public int addStudent ( Student stu) {
return getSqlSession ( ) . getMapper ( StudentDao . class ) . addStudent ( stu) ;
}
@Override
public int updateStudent ( Student stu) {
return getSqlSession ( ) . getMapper ( StudentDao . class ) . updateStudent ( stu) ;
}
@Override
public int deleteStudent ( Student stu) {
return getSqlSession ( ) . getMapper ( StudentDao . class ) . deleteStudent ( stu) ;
}
}