笔记60 Spring+Mybatis整合

整合思路:将SessionFactory交给Spring管理,并且把Mapper和XML结合起来使用。

一、目录结构

二、基本的pojo

Category.java

 1 package com.pojo;
 2 
 3 public class Category {
 4     private int id;
 5     private String name;
 6 
 7     public int getId() {
 8         return id;
 9     }
10 
11     public void setId(int id) {
12         this.id = id;
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public String toString() {
24         return "Category [id=" + id + ", name=" + name + "]";
25     }
26 }

三、Mapper

在这里使用动态SQL语句,需要新增CategoryDynaSqlProvider,提供CRUD对应的SQL语句。

CategoryMapper.java

 1 package com.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.DeleteProvider;
 6 import org.apache.ibatis.annotations.InsertProvider;
 7 import org.apache.ibatis.annotations.SelectProvider;
 8 import org.apache.ibatis.annotations.UpdateProvider;
 9 
10 import com.dynasql.CategoryDynaSqlProvider;
11 import com.pojo.Category;
12 
13 public interface CategoryMapper {
14     @InsertProvider(type = CategoryDynaSqlProvider.class, method = "add")
15     public int add(Category category);
16 
17     @DeleteProvider(type = CategoryDynaSqlProvider.class, method = "delete")
18     public void delete(int id);
19 
20     @SelectProvider(type = CategoryDynaSqlProvider.class, method = "get")
21     public Category get(int id);
22 
23     @UpdateProvider(type = CategoryDynaSqlProvider.class, method = "update")
24     public int update(Category category);
25 
26     @SelectProvider(type = CategoryDynaSqlProvider.class, method = "list")
27     public List<Category> list();
28 
29 }

四、CategoryDynaSqlProvider.java

 1 package com.dynasql;
 2 
 3 import org.apache.ibatis.jdbc.SQL;
 4 
 5 public class CategoryDynaSqlProvider {
 6     public String list() {
 7         return new SQL().SELECT("*").FROM("category").toString();
 8     }
 9 
10     public String get() {
11         return new SQL().SELECT("*").FROM("category").WHERE("id=#{id}").toString();
12     }
13 
14     public String add() {
15         return new SQL().INSERT_INTO("category").VALUES("name", "#{name}").toString();
16     }
17 
18     public String update() {
19         return new SQL().UPDATE("category").SET("name=#{name}").WHERE("id=#{id}").toString();
20     }
21 
22     public String delete() {
23         return new SQL().DELETE_FROM("category").WHERE("id=#{id}").toString();
24     }
25 }

或者直接采用xml的方式进行配置:Category.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper
 3     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <mapper namespace="com.mapper.CategoryMapper">
 7     <insert id="add" parameterType="Category">
 8         insert into category ( name ) values (#{name})
 9     </insert>
10 
11     <delete id="delete" parameterType="Category">
12         delete from category where id= #{id}
13     </delete>
14 
15     <select id="get" parameterType="_int" resultType="Category">
16         select * from category where id= #{id}
17     </select>
18 
19     <update id="update" parameterType="Category">
20         update category set name=#{name} where id=#{id}
21     </update>
22     <select id="list" resultType="Category">
23         select * from category
24     </select>
25 </mapper>

五、applicationContext.xml

1.识别注解

1 <context:annotation-config></context:annotation-config>

2.配置数据源

1 <bean name="dataSource"
2         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
3         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
4         <property name="url"
5             value="jdbc:mysql://localhost:3306/sh?characterEncoding=UTF-8"></property>
6         <property name="username" value="root"></property>
7         <property name="password" value="123456"></property>
8 </bean>

3.配置Mybatis的SqlSessionFactory bean,扫描基本的pojo包、加载数据源、扫描配置xml配置文件(如果使用SQL动态语句,这一步可省略)

1     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
2         <property name="typeAliasesPackage" value="com.pojo" />
3         <property name="dataSource" ref="dataSource" />
4         <property name="mapperLocations" value="classpath:com/mapper/*.xml"/>
5     </bean>

4.扫描Mapper类

1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
2         <property name="basePackage" value="com.mapper" />
3 </bean>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6    http://www.springframework.org/schema/beans
 7    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8    http://www.springframework.org/schema/aop
 9    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
10    http://www.springframework.org/schema/tx
11    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12    http://www.springframework.org/schema/context     
13    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
14 
15 
16     <context:annotation-config></context:annotation-config>
17 
18     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
19         <property name="typeAliasesPackage" value="com.pojo" />
20         <property name="dataSource" ref="dataSource" />
21         
22     </bean>
23 
24     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
25         <property name="basePackage" value="com.mapper" />
26     </bean>
27     <!-- 配置数据源 -->
28     <bean name="dataSource"
29         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
30         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
31         <property name="url"
32             value="jdbc:mysql://localhost:3306/sh?characterEncoding=UTF-8"></property>
33         <property name="username" value="root"></property>
34         <property name="password" value="123456"></property>
35     </bean>
36 </beans>

六、测试

 1 package com.test;
 2 
 3 import java.util.List;
 4 
 5 import org.junit.runner.RunWith;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 import com.mapper.CategoryMapper;
11 import com.pojo.Category;
12 
13 @RunWith(SpringJUnit4ClassRunner.class)
14 @ContextConfiguration("classpath:applicationContext.xml")
15 public class Test {
16 
17     @Autowired
18     private CategoryMapper categoryMapper;
19 
20     @org.junit.Test
21     public void testAdd() {
22         Category category = new Category();
23         category.setName("new Category");
24         categoryMapper.add(category);
25     }
26 
27     @org.junit.Test
28     public void testList() {
29         System.out.println(categoryMapper);
30         List<Category> cs = categoryMapper.list();
31         for (Category c : cs) {
32             System.out.println(c.getName());
33         }
34     }
35 }
posted @ 2018-07-03 14:45  雨落忧伤-  阅读(174)  评论(0编辑  收藏  举报