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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.openlab</groupId>
<artifactId>springdemo01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.4.1</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.5</version>
</dependency>
<!--Spring事物依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
2.properties文件
driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/web name=root password=123456
3.Bean层编写
package org.openlab.bean; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class User { private String id; private String username; private String password; private Integer roleId; private String iconURL; }
4.Dao层的接口的编写(相当于mabstis的mapper包中的接口文件)
package org.openlab.dao; import org.openlab.bean.User; import java.util.List; public interface UserDao { public void save(User user); public List<User> findAll(); }
5.Service层的接口及实现类的编写
package org.openlab.service; import org.openlab.bean.User; import java.util.List; public interface UserService { public List<User> findAll(); }
实现类
package org.openlab.service.impl; import org.openlab.bean.User; import org.openlab.dao.UserDao; import org.openlab.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userdao; @Override public List<User> findAll() { return userdao.findAll(); } }
6.映射文件xml(相当于mybatis中的mapper映射文件)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="org.openlab.dao.UserDao"> <!-- public void save(User user)--> <insert id="save"> INSERT INTO user VALUES(#{id},#{username},#{password},#{roleId},#{iconURL}) </insert> <!--public List<User> findAll()--> <select id="findAll" resultType="org.openlab.bean.User"> select * from user </select> </mapper>
7.mybatis-config.xml文件
<?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> <!--数据源:交由spring注入--> <typeAliases> <package name="org.openlab.bean"/> </typeAliases> <!-- 映射:交由spring注入--> </configuration>
8.整合Spring-Mybatis.xml(有详细的解释再每个bean的开头)
<?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: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 https://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 https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--开启扫描--> <context:component-scan base-package="org.openlab"></context:component-scan> <!--扫描配置文件--> <context:property-placeholder location="classpath:druid.properties"></context:property-placeholder> <!--将配置文件中的参数注入.DruidDataSource之中--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${driverClassName}"></property> <property name="url" value="${url}"></property> <property name="username" value="${name}"></property> <property name="password" value="${password}"></property> </bean> <!--将数据源注入事务管理者之中--> <bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--将数据源、映射的文件被映射的地址、配置文件的地址注入到SqlSessionfactory,准备由其生成Sqlsession--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:org/openlab/dao/*.xml"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!--SqlSessionfactor的构造方法构造SqlSession对象--> <bean class="org.mybatis.spring.SqlSessionTemplate" id="sessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> </bean> <!--由Sqlsession得到mapper对象,并将所得的对象注入到basePackage所指向的路径--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.openlab.dao"></property> <property name="sqlSessionTemplateBeanName" value="sessionTemplate"></property> </bean> <!--开启事务,并将所有方法方法的事务等级蛇者为REQUIRED,将find开头和get开头的方法设置为只读--> <tx:advice id="TxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"></tx:method> <tx:method name="get*" read-only="true"></tx:method> </tx:attributes> </tx:advice> <!--配置那些方法要被事务执行,--> <aop:config> <aop:pointcut id="point" expression="execution(* org.openlab.service.impl.*.*(..))"/> <aop:advisor advice-ref="TxAdvice" pointcut-ref="point"></aop:advisor> </aop:config> </beans>
9.测试类test
import org.junit.Test; import org.openlab.bean.User; import org.openlab.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class mybatisAndspring { @Test public void test(){ ApplicationContext context=new ClassPathXmlApplicationContext("mybatis-Spring.xml"); UserService userService=context.getBean(UserService.class); List<User> list=userService.findAll(); for (User user:list ) { System.out.println(user.getId()+"\t"+user.getUsername()); } } }