1、在pom.xml中导入Jar文件
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency>
2、 MyBatis和Spring整合(XML版)
(1)实体类Accounts
package cn.spring.springandmybatis.entity; public class Accounts { private Integer accountid; private String accountname; private double balance; public Integer getAccountid() { return accountid; } public void setAccountid(Integer 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; } }
(2)AccountsDao
package cn.spring.springandmybatis.dao; import cn.spring.springandmybatis.entity.Accounts; import java.util.List; public interface AccountsDao { List<Accounts> getAll(); }
(3)AccountsDao.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"> <!-- namespace属性代表该Mapper文件的唯一标识,通常习惯设置mapper接口名--> <mapper namespace="cn.spring.springandmybatis.dao.AccountsDao"> <select id="getAll" resultType="Accounts"> select * from accounts; </select> </mapper>
(4)AccountsService
package cn.spring.springandmybatis.service; import cn.spring.springandmybatis.entity.Accounts; import java.util.List; public interface AccountsService { List<Accounts> getAll(); }
(5)AccountsServiceImpl
package cn.spring.springandmybatis.service.impl; import cn.spring.springandmybatis.dao.AccountsDao; import cn.spring.springandmybatis.entity.Accounts; import cn.spring.springandmybatis.service.AccountsService; import java.util.List; public class AccountsServiceImpl implements AccountsService { AccountsDao accountsDao; @Override public List<Accounts> getAll() { return accountsDao.getAll(); } public AccountsDao getAccountsDao() { return accountsDao; } public void setAccountsDao(AccountsDao accountsDao) { this.accountsDao = accountsDao; } }
(6)jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/account?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=123
(7)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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop.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"> <!--1.导入jdbc.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="jdbc.properties"></property> </bean> <!--2.配置数据源 spring内置的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--3.配置mybatis的核心对象SqlSessionFatcoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="cn.spring.springandmybatis.entity"></property> <property name="mapperLocations" value="classpath*:mapper/*xml"></property> </bean> <!--4.Mybatis的Dao接口的包扫描器(借助Dao接口生成动态代理)--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.spring.springandmybatis.dao"></property> </bean> <!--5.将service实现类对象声明到Spring容器中--> <bean id="accountsService" class="cn.spring.springandmybatis.service.impl.AccountsServiceImpl" autowire="byType"></bean> <!--6.事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="transactionAdvice" transaction-manager="transactionManager" > <tx:attributes> <tx:method name="getAll" isolation="READ_COMMITTED" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* *..service.impl.*.*(..))"></aop:advisor> </aop:config> <!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"></context:component-scan> </beans>
(8)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> <!--配置别名--> <typeAliases> <package name="cn.spring.springandmybatis.entity"/> </typeAliases> <!--加载小配置文件--> <mappers> <package name="cn.spring.springandmybatis.dao"/> </mappers> </configuration>
(9)测试类
package cn.spring; import cn.spring.springandmybatis.entity.Accounts; import cn.spring.springandmybatis.service.AccountsService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class SpringAndMybatisTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountsService bean = context.getBean(AccountsService.class); List<Accounts> all = bean.getAll(); for (Accounts account:all) { System.out.println("姓名:"+account.getAccountname()); } } }
(10)控制台
3、 MyBatis和Spring整合(注解版)
(1)实体类Accounts
代码实现
(2)AccountsDao
package cn.spring.springandmybatis.dao; import cn.spring.springandmybatis.entity.Accounts; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface AccountsDao { List<Accounts> getAll(); }
(3)AccountsDao.xml
代码实现
(4)AccountsService
代码实现
(5)AccountsServiceImpl
package cn.spring.springandmybatis.service.impl; import cn.spring.springandmybatis.dao.AccountsDao; import cn.spring.springandmybatis.entity.Accounts; import cn.spring.springandmybatis.service.AccountsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("accountsService") public class AccountsServiceImpl implements AccountsService { @Autowired AccountsDao accountsDao; @Override public List<Accounts> getAll() { return accountsDao.getAll(); } public AccountsDao getAccountsDao() { return accountsDao; } public void setAccountsDao(AccountsDao accountsDao) { this.accountsDao = accountsDao; } }
(6)jdbc.properties
代码实现
(7)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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop.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"> <!--1.导入jdbc.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="jdbc.properties"></property> </bean> <!--2.配置数据源 spring内置的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--3.配置mybatis的核心对象SqlSessionFatcoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="cn.spring.springandmybatis.entity"></property> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--4.Mybatis的Dao接口的包扫描器(借助Dao接口生成动态代理)--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.spring.springandmybatis.dao"></property> </bean> <!--5.事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="transactionAdvice" transaction-manager="transactionManager" > <tx:attributes> <tx:method name="getAll" isolation="READ_COMMITTED" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* *..service.impl.*.*(..))"></aop:advisor> </aop:config> <!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"></context:component-scan> </beans>
(8)mybatis-config.xml
代码实现
(9)测试类
package cn.spring; import cn.spring.springandmybatis.entity.Accounts; import cn.spring.springandmybatis.service.AccountsService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class SpringAndMybatisTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountsService bean = (AccountsService)context.getBean("accountsService"); List<Accounts> all = bean.getAll(); for (Accounts account:all) { System.out.println("姓名:"+account.getAccountname()); } } }
(10)控制台