1:导入依赖包:
<?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">
<parent>
<artifactId>sm</artifactId>
<groupId>com.imooc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sm_service</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectJweaver</artifactId>
<version>1.8.0</version>
</dependency>
<!--Spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
2:建立数据库对应实体类:
package com.imooc.sm.entity;
public class Department {
private Integer id;
private String name;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3:建立用于增删该查的Dao接口。
package com.imooc.sm.dao;
import com.imooc.sm.entity.Department;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("departmentDao")
public interface DepartmentDao {
void insert(Department department);
void delete(Integer id);
void update(Department department);
Department selectById(Integer id);
List<Department> selectAll();
}
4:配置Spring文件
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Spring整合Mybatis -->
<!--配置数据源-->
<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/sm?useUnicode=true&characterEncoding=utf-8&useSSL=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置用于Spring的SqlSession工厂类-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--创建SqlSessionFactory实例需要提供数据源配置,这里以属性添加-->
<property name="dataSource" ref="dataSource"/>
<!--这里配置一个包,mybatis会自动扫描包下面的javabean,并且默认起一个首字母小写的别名-->
<property name="typeAliasesPackage" value="com.imooc.sm.entity"/>
</bean>
<!--整合进spring-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定Dao包位置-->
<property name="basePackage" value="com.imooc.sm.dao"/>
<!--指定SqlSessionFactory的实例名。这里用的是名字,不用引用-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--配置声明式事务-->
<!--配置事务管理器,需要添加一个数据源属性-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--相当于定义事务的增强通知,这里tx:advice是一个事务增强的集合,tx:method是具体的增强通知。找到切入点后根据name进行匹配织入-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--定义切入点-->
<aop:pointcut id="txPonitcut" expression="execution(* com.imooc.sm.service.*.*(..))"/>
<!--切入点和增强整合成切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPonitcut"/>
</aop:config>
<!--全局扫描-->
<context:component-scan base-package="com.imooc.sm"/>
<!--开启AspectJ自动代理-->
<aop:aspectj-autoproxy/>
</beans>
5:配置mapper文件:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.sm.dao.DepartmentDao">
<resultMap id="resultMap" type="com.imooc.sm.entity.Department">
<id property="id" column="id" javaType="Integer"/>
<result property="name" column="name" javaType="String"/>
<result property="address" column="address" javaType="String"/>
</resultMap>
<insert id="insert" parameterType="com.imooc.sm.entity.Department" useGeneratedKeys="true">
insert into department(name,address) values(#{name},#{address})
</insert>
<delete id="delete" parameterType="Integer">
delete from department where id=#{id}
</delete>
<update id="update" parameterType="com.imooc.sm.entity.Department">
update department set name=#{name},address=#{address} where id=#{id}
</update>
<select id="selectById" parameterType="Integer" resultMap="resultMap">
select * from department where id=#{id}
</select>
<select id="selectAll" resultMap="resultMap">
select * from department
</select>
</mapper>
其他的一些业务实现就不写了。以上是整合spring-mybatis的核心部分。