Spring+JDBC+Mybatis-0.1

Mybatis 整合 Spring

一、文件结构

二,Java 代码

 1 package mybatis.entity;
 2 
 3 /**
 4  * 该类为实体类,与dept表对应 类---表 对象---记录 成员变量---字段
 5  * 
 6  * ORM:对象关系型数据映射 mybatis hibernate
 7  *
 8  */
 9 public class Entity {
10     private int id;
11     private String name;
12     private String sex;
13 
14     public Entity() {
15         super();
16     }
17 
18     public Entity(int id, String name, String sex) {
19         super();
20         this.id = id;
21         this.name = name;
22         this.sex = sex;
23     }
24 
25     public int getId() {
26         return id;
27     }
28 
29     public void setId(int id) {
30         this.id = id;
31     }
32 
33     public String getName() {
34         return name;
35     }
36 
37     public void setName(String name) {
38         this.name = name;
39     }
40 
41     public String getSex() {
42         return sex;
43     }
44 
45     public void setSex(String sex) {
46         this.sex = sex;
47     }
48 
49     @Override
50     public String toString() {
51         return "Entity [id=" + id + ", name=" + name + ", sex=" + sex + "]";
52     }
53 
54 }
Entity

  Mybatis 文件

 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

  Java代码

 1 package mybatis.mapper;
 2 
 3 import java.util.List;
 4 
 5 import mybatis.entity.Entity;
 6 
 7 public interface Mapper {
 8     public List<Entity> selectAll();
 9 
10     public int insertParams(Entity en);
11 
12     public Entity selectById(int id);
13 
14     public int delById(int id);
15 
16     public List<Entity> selectByLike(String keyWord);
17 }
Mapper
 1 package service.inter;
 2 
 3 import java.util.List;
 4 
 5 import mybatis.entity.Entity;
 6 
 7 public interface ServiceInter {
 8     public List<Entity> selectAll();
 9     public int insertParams(Entity en);
10     public Entity selectById(int id);
11     public int delById(int id);
12     public List<Entity> selectByLike(String keyWord);
13 }
ServiceInter
 1 package service.impl;
 2 
 3 import java.util.List;
 4 
 5 import mybatis.entity.Entity;
 6 import mybatis.mapper.Mapper;
 7 import service.inter.ServiceInter;
 8 
 9 /**
10  * 事务做用在该层
11  * 
12  * @author Administrator
13  *
14  */
15 public class ServiceImpl implements ServiceInter {
16     public Mapper mapper;
17     
18     public Mapper getMapper() {
19         return mapper;
20     }
21 
22     public void setMapper(Mapper mapper) {
23         this.mapper = mapper;
24     }
25 
26     public List<Entity> selectAll() {
27         return mapper.selectAll();
28     }
29 
30     public int insertParams(Entity en) {
31         return this.getMapper().insertParams(en);
32     }
33 
34     public Entity selectById(int id) {
35         return mapper.selectById(id);
36     }
37 
38     public int delById(int id) {
39         return mapper.delById(id);
40     }
41 
42     public List<Entity> selectByLike(String keyWord) {
43         return mapper.selectByLike(keyWord);
44     }
45 
46 }
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 
 8 import mybatis.entity.Entity;
 9 import service.inter.ServiceInter;
10 
11 public class MybatisTest {
12     public static void main(String[] args) {
13         insertParams();
14         selectAll();
15     }
16 
17     public static void insertParams() {
18         // 加载ioc容器
19         ApplicationContext context = new ClassPathXmlApplicationContext(
20                 new String[] { "applicationContext-context.xml", "applicationContext-mybatis.xml" });
21         ServiceInter service = (ServiceInter) context.getBean("ServiceImpl");
22         Entity en=new Entity();
23         en.setId(402);
24         en.setName("比克");
25         en.setSex("男");
26         int flag = service.insertParams(en);
27 
28     }
29 
30     public static void selectAll() {
31         // 加载ioc容器
32         ApplicationContext context = new ClassPathXmlApplicationContext(
33                 new String[] { "applicationContext-context.xml", "applicationContext-mybatis.xml" });
34         ServiceInter service = (ServiceInter) context.getBean("ServiceImpl");
35         List<Entity> list = service.selectAll();
36         for (Entity d : list) {
37             System.out.println(d);
38         }
39     }
40 }
MybatisTest

三、配置文件

 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_Mybatis</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
1 driverClass=com.mysql.cj.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai
3 name=root
4 password=123456
db.properties
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>
mybatisConfig.xml
 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" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 8     http://www.springframework.org/schema/context
 9     http://www.springframework.org/schema/context/spring-context-4.3.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
12     http://www.springframework.org/schema/tx
13     http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
14     <!-- service -->
15     <bean id="ServiceImpl" class="service.impl.ServiceImpl">
16         <property name="mapper" ref="mapper"></property>
17     </bean>
18 </beans>
applicationContext-context.xml
 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" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 8     http://www.springframework.org/schema/context
 9     http://www.springframework.org/schema/context/spring-context-4.3.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
12     http://www.springframework.org/schema/tx
13     http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
14     <!-- 加载数据库配置文件 -->
15     <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
16     <!-- 创建数据源 -->
17     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
18         init-method="init" destroy-method="close">
19         <!-- 配置数据库连接基本信息 -->
20         <property name="driverClassName" value="${driverClass}" />
21         <property name="url" value="${url}" />
22         <property name="username" value="${name}" />
23         <property name="password" value="${password}" />
24         <!-- ******配置数据库连接池相关信息******* -->
25         <!-- 配置初始化大小、最小、最大 -->
26         <property name="initialSize" value="5" />
27         <property name="minIdle" value="2" />
28         <property name="maxActive" value="10" />
29         <!-- 配置获取连接等待超时的时间 -->
30         <property name="maxWait" value="10000" />
31         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
32         <property name="timeBetweenEvictionRunsMillis" value="60000" />
33         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
34         <property name="minEvictableIdleTimeMillis" value="300000" />
35         <property name="testWhileIdle" value="true" />
36         <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
37         <property name="testOnBorrow" value="true" />
38         <property name="testOnReturn" value="false" />
39         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
40         <property name="poolPreparedStatements" value="true" />
41         <property name="maxPoolPreparedStatementPerConnectionSize"
42             value="20" />
43         <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
44         <property name="defaultAutoCommit" value="true" />
45         <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
46         <property name="validationQuery" value="select 1" />
47     </bean>
48     <!-- SqlSessionFactory -->
49     <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
50         <!-- 注入数据源 -->
51         <property name="dataSource" ref="dataSource"></property>
52         <!-- 加载配置文件 -->
53         <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
54         <!-- 加载映射文件 -->
55         <property name="mapperLocations" value="classpath:mybatis/entity/*.xml"></property>
56     </bean>
57     <!-- 事务管理器 -->
58     <bean id="transactionManager"
59         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
60         <!-- 注入数据源 -->
61         <property name="dataSource" ref="dataSource"></property>
62     </bean>
63     <!-- 通过aop管理事务(事务应该做用在service层) -->
64     <aop:config>
65         <aop:advisor pointcut="execution (* service.impl.*.*(..))"
66             advice-ref="deptAdvice" />
67     </aop:config>
68     <!-- 定义事务通知 -->
69     <tx:advice id="deptAdvice" transaction-manager="transactionManager">
70         <!-- 定义方法对应的事务属性 -->
71         <tx:attributes>
72             <tx:method name="insert*" propagation="REQUIRED"
73                 isolation="READ_COMMITTED" read-only="false" timeout="-1"
74                 rollback-for="Exception" />
75             <tx:method name="delete*" propagation="REQUIRED"
76                 isolation="READ_COMMITTED" read-only="false" timeout="-1"
77                 rollback-for="Exception" />
78             <tx:method name="update*" propagation="REQUIRED"
79                 isolation="READ_COMMITTED" read-only="false" timeout="-1"
80                 rollback-for="Exception" />
81             <tx:method name="select*" propagation="SUPPORTS" read-only="false" rollback-for="Exception" />
82             <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"
83                 read-only="false" timeout="-1" rollback-for="Exception" />
84         </tx:attributes>
85     </tx:advice>
86     <!-- 扫描dao层接口 -->
87     <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
88         <property name="basePackage" value="mybatis.mapper"></property>
89         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
90     </bean>
91 </beans>
applicationContext-mybatis.xml

 

posted @ 2018-09-23 14:59  21yuer  阅读(114)  评论(0编辑  收藏  举报