整合思路:将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. /var/lib/dpkg/lock

    记性不好: Could not get lock /var/lib/dpkg/lock 锁定的文件会阻止 Linux 系统中某些文件或者数据的访问,这个概念也存在于 Windows 或者其他的操作系统 ...

  2. 三、Json方式函数

    一.Json方式函数 // 4. 查看对象信息 console.dir(obj) =>可以显示一个对象所有的属性和方法. var info = { blog: "http://cllg ...

  3. linux 软件管理--yum工具及源码包

    目录 linux 软件管理--yum工具及源码包 一.yum基本概述 二.yum源的配置 三.yum实践案例 四.yum全局配置文件 五.yum签名检查机制 五.制作本地yum仓库 六.构建企业级yu ...

  4. go语言从例子开始之Example28.非阻塞通道操作

    常规的通过通道发送和接收数据是阻塞的.然而,我们可以使用带一个 default 子句的 select 来实现非阻塞 的发送.接收,甚至是非阻塞的多路 select. Example: package ...

  5. 转载 Log4j2在WEB项目中配置

    最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...

  6. Struts 2中的constant详解【转载】

    通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在struts.properties文件中完成. struts.xml 1.&l ...

  7. 0001.第一个多线程demo--分批处理数据

    public class UserEntity { private String userId; private String userName; public String getUserId() ...

  8. Spring 讲解(六)

    如何理解 Spring 中的 AOP 一.AOP 的概述 AOP(Aspect Oriented Programming):面向切面编程,通过预编译方式和运行期动态代理来实现程序功能的统一维护的一种技 ...

  9. InnoDB的LRU淘汰策略

    Reference: https://time.geekbang.org/column/article/121710 InnoDB存储引擎是基于集合索引实现的数据存储,也就是除了索引列以及主键是存储在 ...

  10. 【leetcode】945. Minimum Increment to Make Array Unique

    题目如下: Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. ...