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

一、目录结构

二、基本的pojo

Category.java

 package com.pojo;

 public class Category {
private int id;
private String name; 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 toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}

三、Mapper

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

CategoryMapper.java

 package com.mapper;

 import java.util.List;

 import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider; import com.dynasql.CategoryDynaSqlProvider;
import com.pojo.Category; public interface CategoryMapper {
@InsertProvider(type = CategoryDynaSqlProvider.class, method = "add")
public int add(Category category); @DeleteProvider(type = CategoryDynaSqlProvider.class, method = "delete")
public void delete(int id); @SelectProvider(type = CategoryDynaSqlProvider.class, method = "get")
public Category get(int id); @UpdateProvider(type = CategoryDynaSqlProvider.class, method = "update")
public int update(Category category); @SelectProvider(type = CategoryDynaSqlProvider.class, method = "list")
public List<Category> list(); }

四、CategoryDynaSqlProvider.java

 package com.dynasql;

 import org.apache.ibatis.jdbc.SQL;

 public class CategoryDynaSqlProvider {
public String list() {
return new SQL().SELECT("*").FROM("category").toString();
} public String get() {
return new SQL().SELECT("*").FROM("category").WHERE("id=#{id}").toString();
} public String add() {
return new SQL().INSERT_INTO("category").VALUES("name", "#{name}").toString();
} public String update() {
return new SQL().UPDATE("category").SET("name=#{name}").WHERE("id=#{id}").toString();
} public String delete() {
return new SQL().DELETE_FROM("category").WHERE("id=#{id}").toString();
}
}

或者直接采用xml的方式进行配置:Category.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="com.mapper.CategoryMapper">
<insert id="add" parameterType="Category">
insert into category ( name ) values (#{name})
</insert> <delete id="delete" parameterType="Category">
delete from category where id= #{id}
</delete> <select id="get" parameterType="_int" resultType="Category">
select * from category where id= #{id}
</select> <update id="update" parameterType="Category">
update category set name=#{name} where id=#{id}
</update>
<select id="list" resultType="Category">
select * from category
</select>
</mapper>

五、applicationContext.xml

1.识别注解

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

2.配置数据源

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

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

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

4.扫描Mapper类

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

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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config></context:annotation-config> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.pojo" />
<property name="dataSource" ref="dataSource" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper" />
</bean>
<!-- 配置数据源 -->
<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/sh?characterEncoding=UTF-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>

六、测试

 package com.test;

 import java.util.List;

 import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.mapper.CategoryMapper;
import com.pojo.Category; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test { @Autowired
private CategoryMapper categoryMapper; @org.junit.Test
public void testAdd() {
Category category = new Category();
category.setName("new Category");
categoryMapper.add(category);
} @org.junit.Test
public void testList() {
System.out.println(categoryMapper);
List<Category> cs = categoryMapper.list();
for (Category c : cs) {
System.out.println(c.getName());
}
}
}

笔记60 Spring+Mybatis整合的更多相关文章

  1. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  2. Spring + mybatis整合方案总结 结合实例应用

    Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...

  3. SpringMVC+Spring+Mybatis整合

    SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...

  4. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  5. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...

  6. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

  7. springMVC + Spring + MyBatis 整合

    整理下SSM(基于注解)的整合 1. web.xml 配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  8. struts2 spring mybatis 整合(test)

    这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...

  9. 2.springMVC+spring+Mybatis整合

    前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...

随机推荐

  1. JavaScript如何诞生

    JavaScript之父谈语言诞生记 发表于2011-06-27 10:30| 9749次阅读| 来源ruanyifeng.com| 0 条评论| 作者阮一峰 prototypeprimitiveja ...

  2. Git中.gitignore文件不起作用

    Git中.gitignore文件不起作用的解决以及Git中的忽略规则介绍   在Studio里使用Git管理代码的过程中,可以修改.gitignore文件中的标示的方法来忽略开发者想忽略掉的文件或目录 ...

  3. python读取配置文件(ini、yaml、xml)

    python读取配置文件(ini.yaml.xml)  

  4. 深入理解java虚拟机:笔记

    1.运行时数据区域 1.程序计数器 当前线程执行字节码的行号指示器,字节码解释器工作通过改变这个计数器的值来选取下一条需要执行的字节码指令,每一个线程拥有独立的程序计数器,线程私有的内存 2.虚拟机栈 ...

  5. leetcode-166周赛-5280-用户分组

    题目描述: 自己的提交: class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: ...

  6. Delphi DBGrid 实现复选框

    1 在数据库对应的表中加入  bit 列验证是否被选中 然后dbgrid第一列的filedname是bit列 在DBgrid的onDrawColumnCell事件中写: procedure DBGri ...

  7. SQL Wildcards 通配符

    SQL Wildcards通配符 通配符用于替换字符串中的任何其他字符. 通配符与SQL LIKE运算符一起使用.在WHERE子句中使用LIKE运算符来搜索列中的指定模式. 有两个通配符与LIKE运算 ...

  8. js动态添加的元素绑定事件

    最近做的项目要实现一个动态添加动态删除的功能,思考了一下,该怎么给动态添加的元素绑定事件.最后觉得有两种方式比较可靠,第一种是在动态添加的html代码里添加oclick事件,然后给传个唯一的参数来判断 ...

  9. 【TCP】四次握手原因 / TIME_WAIT作用

    为什么建立TCP连接需要三次握手? 原因:为了应对网络中存在的延迟的重复数组的问题 例子: 假设client发起连接的连接请求报文段在网络中没有丢失,而是在某个网络节点长时间滞留了,导致延迟到达ser ...

  10. 阿里巴巴下一代云分析型数据库AnalyticDB入选Forrester Wave™ 云数仓评估报告 解读

    前言近期, 全球权威IT咨询机构Forrester发布"The Forrester WaveTM: CloudData Warehouse Q4 2018"研究报告,阿里巴巴分析型 ...