配置式
首先导入依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--引入需要的ehcache插件--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>1.2.3</version> </dependency> <!--mybatis整合ehcache的jar--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
entity实体类:
package com.sp.entity; public class Accounts { private int accountid; private String accountname; private double balance; public int getAccountid() { return accountid; } public void setAccountid(int accountid) { this.accountid = accountid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } @Override public String toString() { return "Accounts{" + "accountid=" + accountid + ", accountname='" + accountname + '\'' + ", balance=" + balance + '}'; } }
Dao接口层
package com.sp.dao; import com.sp.entity.Accounts; import java.util.List; public interface AccountDao { //查询全部 List<Accounts> getAll(); }
Dao.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指定Dao接口的完整类名 mybatis会依据这个接口动态创建一个实现类去实现这个接口, 而这个实现类是一个Mapper对象--> <mapper namespace="com.sp.dao.AccountDao"> <select id="getAll" resultType="com.sp.entity.Accounts"> SELECT * FROM accounts </select> </mapper>
Service业务接口层:
package com.sp.service; import com.sp.entity.Accounts; import java.util.List; public interface AccountService { //查询全部 List<Accounts> getAll(); }
ServiceImpl业务接口实现层:
package com.sp.serviceimpl; import com.sp.dao.AccountDao; import com.sp.entity.Accounts; import com.sp.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.List; public class AccountServiceImpl implements AccountService { //植入对象 AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; }
@Override public List<Accounts> getAll() { return accountDao.getAll(); }
jdbc.properties文件:
#数据源配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql:///account user=root password=123
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--导入jdbc.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application.properties"></property> </bean> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!--配置mybatis核心对象SqlSessionFactory--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="com.sp.entity"></property> <property name="mapperLocations" value="classpath*:mapper/*.xml"></property> <!-- <property name="configLocation" value="classpath:config.xml"></property>--> </bean> <!--mybatis接口的包的扫描器 生成动态代理--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.sp.dao"></property> </bean> <!--将service实现类声明到spring容器中--> <!--隐式注入--> <!--byType:根据serviceImpl实现类中的植入对象获取的--> <bean id="accountServiceImpl" class="com.sp.serviceimpl.AccountServiceImpl" autowire="byType"></bean> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" isolation="READ_COMMITTED" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--切面--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.sp.service.*.*(..))"></aop:advisor> </aop:config> </beans>
测试:
@Test public void test01(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); com.sp.service.AccountService accountServiceImpl = ctx.getBean(com.sp.service.AccountService.class); System.out.println(accountServiceImpl.getAll()); }
注解式
entity实体类:
package com.sp.entity; public class Accounts { private int accountid; private String accountname; private double balance; public int getAccountid() { return accountid; } public void setAccountid(int accountid) { this.accountid = accountid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } @Override public String toString() { return "Accounts{" + "accountid=" + accountid + ", accountname='" + accountname + '\'' + ", balance=" + balance + '}'; } }
Dao层接口类:
package com.sp.dao;
import com.sp.entity.Accounts; import java.util.List; public interface AccountDao { //查询全部 List<Accounts> getAll(); }
Dao层小配置文件:
<?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="com.sp.dao.AccountDao"> <select id="getAll" resultType="com.sp.entity.Accounts"> SELECT * FROM accounts </select> </mapper>
Service接口类:
package com.sp.service;
import com.sp.entity.Accounts; import java.util.List; public interface AccountService { //查询全部 List<Accounts> getAll(); }
Service接口实现类:
package com.sp.serviceimpl;
import com.sp.dao.AccountDao; import com.sp.entity.Accounts; import com.sp.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service("accountServiceImpl") public class AccountServiceImpl implements AccountService { //植入对象 @Autowired AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } /*事务的隔离级别在传播行为*/ @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) @Override public List<Accounts> getAll() { return accountDao.getAll(); } }
jdbc.properties文件:
#数据源配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql:///account user=root password=123
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--导入jdbc.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application.properties"></property> </bean> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!--配置mybatis核心对象SqlSessionFactory--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="com.sp.entity"></property> <property name="mapperLocations" value="classpath*:mapper/*.xml"></property> <!-- <property name="configLocation" value="classpath:config.xml"></property>--> </bean> <!--mybatis接口的包的扫描器 生成动态代理--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.sp.dao"></property> </bean> <!--开启注解配置和事务配置--> <context:component-scan base-package="com.sp"></context:component-scan> <!--开启事务的支持--> <context:annotation-config/> <!--开启事务注解的支持--> <tx:annotation-driven/> </beans>
测试:
@Test public void test01(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); com.sp.service.AccountService accountServiceImpl = ctx.getBean(com.sp.service.AccountService.class); System.out.println(accountServiceImpl.getAll()); }