-------------------------------mybatis和spring之间经典整合
Posted 【宁静的夏天】
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了-------------------------------mybatis和spring之间经典整合相关的知识,希望对你有一定的参考价值。
Spring整合MyBatis 思想:整合一定不是两个框架jar包简单的堆砌 真正的思想是Spring接管了MyBatis框架的一部分工作 1.SqlSessionFactory 创建权交给Spring 生成SqlSession,打通通道 2.如果你想将咱们的DAO配置到Spring。接口实现类 <bean id="bookDAO" class="MapperScannerConfig"> 你接口的命名规范起到非常重要的作用 接口以I开头,生成对象就是接口名称本身 IBookDAO IBookDAO 没有以I开头, BookDAO 对象的名称 bookDAO 整合最根本的点: 1.Spring生成工厂 SqlSessionFactoryBean------>dataSource 和mybatis-config.xml Spring 整合JavaWeb spring-web
整体结构如下图:
1、配置文件:
1>applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!--01.加载jdbc.properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--02.配置数据源 002.使用dbcp数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.jdbcUrl}"/> <property name="username" value="${jdbc.userName}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--03. 配置sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatisContext.xml"/> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件,**表示迭代查找 ,,也可在mybatis-config.xml中单独指定xml文件 <property name="mapperLocations" value="classpath:cn/bdqn/dao/*.xml"/> --> </bean> <!-- 04.配置userDao--> <!-- <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.bdqn.dao.UserDao"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.bdqn.dao"/> </bean> <!--05.配置service--> <bean id="userService" class="cn.bdqn.service.impl.UserServiceImpl"> <property name="userDao" ref="IUserDao"/> </bean> <!--06.配置事务--> <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="add*" isolation="DEFAULT" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="mypoint" expression="execution(* *..service.*.add*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/> </aop:config> <!--扫描注解定义--> <!--<context:component-scan base-package="cn.bdqn.service"/>--> </beans>
2>jdbc.properties外置文件:
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///mybatis jdbc.userName=root jdbc.password=000
3>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.bdqn.bean"/> </typeAliases> <!--可以省略<mappers> <package name="cn.bdqn.dao"/> </mappers>--> </configuration>
4>IUserDao.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.bdqn.dao.IUserDao"> <insert id="addUser"> INSERT into user(name,address,birthday) VALUES (#{name},#{address},#{birthday}) </insert> </mapper>
5>pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--spring对应的版本号--> <spring.version>4.2.1.RELEASE</spring.version> </properties> <dependencies> <!-- mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--aspectj需要的jar--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <!--spring整合aspect需要的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!--spring事务管理需要的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!--aop联盟--> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!--spring-aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!--引入需要的spring 核心jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!--Apache 的dbcp依赖--> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--ServletAPI--> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
1>实体类Student
public class User implements Serializable { private int id; private String name; private String address; private Date birthday; public User() { } public User(int id, String name, String address, Date birthday) { this.id = id; this.name = name; this.address = address; this.birthday = birthday; } public int getId() { return id; } public void setId(int 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; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "id=" + id + ", name=\'" + name + \'\\\'\' + ", address=\'" + address + \'\\\'\' + ", birthday=" + birthday + \'}\'; } }
2>dao接口
public interface IUserDao { /** * 添加用户 * @param user user对象 * @return 影响横数 */ int addUser(User user); }
3>service
public interface UserService { int addUser(User user);
4.>serviceImpl
public class UserServiceImpl implements UserService{ private IUserDao userDao; public int addUser(User user) { return userDao.addUser(user); } public IUserDao getUserDao() { return userDao; } public void setUserDao(IUserDao userDao) { this.userDao = userDao; } }
5.test:
public class test01 { @Test public void test(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) context.getBean("userService"); User user=new User(); user.setName("小明"); user.setAddress("五道口"); user.setBirthday(new Date()); userService.addUser(user); } }
以上是关于-------------------------------mybatis和spring之间经典整合的主要内容,如果未能解决你的问题,请参考以下文章