Spring+JDBC+Mybatis-0.2

通过注解方式实现 Mybatis 整合 Spring 

 

一、文件结构

二,Java 代码

Entity
Mapper
ServiceInter

使用注解方式后的修改

 1 package service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 import org.springframework.transaction.annotation.Isolation;
 8 import org.springframework.transaction.annotation.Propagation;
 9 import org.springframework.transaction.annotation.Transactional;
10 
11 import mybatis.entity.Entity;
12 import mybatis.mapper.Mapper;
13 import service.inter.ServiceInter;
14 
15 /**
16  * 事务做用在该层
17  * 
18  * @author Administrator
19  *
20  */
21 @Service("ServiceImpl")
22 public class ServiceImpl implements ServiceInter {
23     @Autowired
24     public Mapper mapper;
25 
26     public Mapper getMapper() {
27         return mapper;
28     }
29 
30     public void setMapper(Mapper mapper) {
31         this.mapper = mapper;
32     }
33 
34     public List<Entity> selectAll() {
35         return mapper.selectAll();
36     }
37 
38     @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, readOnly = false, timeout = -1, noRollbackFor = Exception.class)
39     public int insertParams(Entity en) {
40         return this.getMapper().insertParams(en);
41     }
42 
43     @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, readOnly = false, timeout = -1, noRollbackFor = Exception.class)
44     public Entity selectById(int id) {
45         return mapper.selectById(id);
46     }
47 
48     @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, readOnly = false, timeout = -1, noRollbackFor = Exception.class)
49     public int delById(int id) {
50         return mapper.delById(id);
51     }
52 
53     @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, readOnly = false, timeout = -1, noRollbackFor = Exception.class)
54     public List<Entity> selectByLike(String keyWord) {
55         return mapper.selectByLike(keyWord);
56     }
57 
58 }
ServiceImpl
 1 package spring.mybatis.test;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 import org.springframework.stereotype.Service;
 8 
 9 import mybatis.entity.Entity;
10 import service.inter.ServiceInter;
11 
12 public class MybatisTest {
13     public static void main(String[] args) {
14         insertParams();
15         selectAll();
16     }
17 
18     public static void insertParams() {
19         // 加载ioc容器
20         ApplicationContext context = new ClassPathXmlApplicationContext(
21                 new String[] { "applicationContext-context.xml", "applicationContext-mybatis.xml" });
22         // 寻找@Service("ServiceImpl")
23         ServiceInter service = (ServiceInter) context.getBean("ServiceImpl");
24         Entity en = new Entity();
25         en.setId(404);
26         en.setName("那巴");
27         en.setSex("男");
28         int flag = service.insertParams(en);
29 
30     }
31 
32     public static void selectAll() {
33         // 加载ioc容器
34         ApplicationContext context = new ClassPathXmlApplicationContext(
35                 new String[] { "applicationContext-context.xml", "applicationContext-mybatis.xml" });
36         ServiceInter service = (ServiceInter) context.getBean("ServiceImpl");
37         List<Entity> list = service.selectAll();
38         for (Entity d : list) {
39             System.out.println(d);
40         }
41     }
42 }
MybatisTest

三、配置文件

1.maven 导包

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>spring</groupId>
 6     <artifactId>spring_JDBC4_Mybatis2</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <dependencies>
 9         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
10         <dependency>
11             <groupId>org.mybatis</groupId>
12             <artifactId>mybatis</artifactId>
13             <version>3.4.5</version>
14         </dependency>
15         <!-- mybatis整合spring时的插件 -->
16         <dependency>
17             <groupId>org.mybatis</groupId>
18             <artifactId>mybatis-spring</artifactId>
19             <version>1.3.2</version>
20         </dependency>
21         <!-- druid数据源 -->
22         <dependency>
23             <groupId>com.alibaba</groupId>
24             <artifactId>druid</artifactId>
25             <version>1.1.8</version>
26         </dependency>
27         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
28         <dependency>
29             <groupId>mysql</groupId>
30             <artifactId>mysql-connector-java</artifactId>
31             <version>8.0.11</version>
32         </dependency>
33 
34         <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
35         <dependency>
36             <groupId>org.springframework</groupId>
37             <artifactId>spring-tx</artifactId>
38             <version>4.3.2.RELEASE</version>
39         </dependency>
40         <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
41         <dependency>
42             <groupId>org.springframework</groupId>
43             <artifactId>spring-jdbc</artifactId>
44             <version>4.3.2.RELEASE</version>
45         </dependency>
46         <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
47         <dependency>
48             <groupId>org.springframework</groupId>
49             <artifactId>spring-core</artifactId>
50             <version>4.3.2.RELEASE</version>
51         </dependency>
52         <dependency>
53             <groupId>org.springframework</groupId>
54             <artifactId>spring-context</artifactId>
55             <version>4.3.2.RELEASE</version>
56         </dependency>
57         <dependency>
58             <groupId>org.springframework</groupId>
59             <artifactId>spring-beans</artifactId>
60             <version>4.3.2.RELEASE</version>
61         </dependency>
62         <dependency>
63             <groupId>org.springframework</groupId>
64             <artifactId>spring-expression</artifactId>
65             <version>4.3.2.RELEASE</version>
66         </dependency>
67         <!--使用AspectJ方式注解需要相应的包 -->
68         <dependency>
69             <groupId>org.aspectj</groupId>
70             <artifactId>aspectjrt</artifactId>
71             <version>1.6.11</version>
72         </dependency>
73         <!--使用AspectJ方式注解需要相应的包 -->
74         <dependency>
75             <groupId>org.aspectj</groupId>
76             <artifactId>aspectjweaver</artifactId>
77             <version>1.6.11</version>
78         </dependency>
79     </dependencies>
80 </project>
POM.xml

2.mybatis配置文件

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
4 <configuration>
5 
6 </configuration>
mybatis.xml

3.Spring配置文件

  3.1

  
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
10     http://www.springframework.org/schema/context
11     http://www.springframework.org/schema/context/spring-context-4.3.xsd
12     http://www.springframework.org/schema/aop
13     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
14     http://www.springframework.org/schema/tx
15     http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
16     <!-- 加载数据库配置文件 -->
17     <context:property-placeholder
18         location="classpath:db.properties"></context:property-placeholder>
19     <!-- 创建数据源 -->
20     <bean id="dataSource"
21         class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
22         destroy-method="close">
23         <!-- 配置数据库连接基本信息 -->
24         <property name="driverClassName" value="${driverClass}" />
25         <property name="url" value="${url}" />
26         <property name="username" value="${name}" />
27         <property name="password" value="${password}" />
28         <!-- ******配置数据库连接池相关信息******* -->
29         <!-- 配置初始化大小、最小、最大 -->
30         <property name="initialSize" value="5" />
31         <property name="minIdle" value="2" />
32         <property name="maxActive" value="10" />
33         <!-- 配置获取连接等待超时的时间 -->
34         <property name="maxWait" value="10000" />
35         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
36         <property name="timeBetweenEvictionRunsMillis" value="60000" />
37         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
38         <property name="minEvictableIdleTimeMillis" value="300000" />
39         <property name="testWhileIdle" value="true" />
40         <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
41         <property name="testOnBorrow" value="true" />
42         <property name="testOnReturn" value="false" />
43         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
44         <property name="poolPreparedStatements" value="true" />
45         <property name="maxPoolPreparedStatementPerConnectionSize"
46             value="20" />
47         <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
48         <property name="defaultAutoCommit" value="true" />
49         <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
50         <property name="validationQuery" value="select 1" />
51     </bean>
52     <!-- SqlSessionFactory -->
53     <bean id="sqlSessionFactoryBean"
54         class="org.mybatis.spring.SqlSessionFactoryBean">
55         <!-- 注入数据源 -->
56         <property name="dataSource" ref="dataSource"></property>
57         <!-- 加载配置文件 -->
58         <property name="configLocation"
59             value="classpath:mybatisConfig.xml"></property>
60         <!-- 加载映射文件 -->
61         <property name="mapperLocations"
62             value="classpath:mybatis/entity/*.xml"></property>
63     </bean>
64     <!-- 事务管理器 -->
65     <bean id="transactionManager"
66         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
67         <!-- 注入数据源 -->
68         <property name="dataSource" ref="dataSource"></property>
69     </bean>
70     <!-- 启用基于注解实现事务管理 -->
71     <tx:annotation-driven
72         transaction-manager="transactionManager" />
73     <!-- 扫描dao层接口 -->
74     <bean id="mapperScannerConfigurer"
75         class="org.mybatis.spring.mapper.MapperScannerConfigurer">
76         <property name="basePackage" value="mybatis.mapper"></property>
77         <property name="sqlSessionFactoryBeanName"
78             value="sqlSessionFactoryBean"></property>
79     </bean>
80 </beans>
applicationContext-mybatis.xml

  3.2映射文件

  
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 <mapper namespace="mybatis.mapper.Mapper">
 5     <!-- 查询全部 -->
 6     <select id="selectAll" resultType="mybatis.entity.Entity">
 7         select * from person2
 8     </select>
 9     
10     <!-- 传参添加部门信息 -->
11     <insert id="insertParams" parameterType="mybatis.entity.Entity">
12         INSERT INTO person2
13         VALUES(#{id},#{name},#{sex})
14     </insert>
15 
16 
17     <!-- ID查询 -->
18     <select id="selectById" parameterType="int" resultType="mybatis.entity.Entity">
19         select * from person2
20         where id=#{id}
21     </select>
22 
23     <!-- id删除 -->
24     <delete id="delById" parameterType="int">
25         delete from person2 where
26         id=#{id}
27     </delete>
28     <!-- id更新 -->
29     <update id="updById" parameterType="mybatis.entity.Entity">
30         update person2 set
31         name=#{name},sex=#{sex} where id=#{id}
32     </update>
33 
34     <!-- 模糊查询 -->
35     <select id="selectByLike" parameterType="java.lang.String"
36         resultType="mybatis.entity.Entity">
37         SELECT * FROM person2 WHERE name LIKE CONCAT('%',#{keyWord},'%')
38     </select>
39 </mapper>
EntityMapper

 

posted @ 2018-09-23 16:05  21yuer  阅读(123)  评论(0编辑  收藏  举报