整合思路:将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. lsyncd+rsync文件实时同步

    1.rsync两端都需要安装 yum -y install rsync 2.提供lsyncd的安装源 rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x ...

  2. 利用Stream模式进行文件拷贝

    const fs = require('fs'); const file = fs.createReadStream("readfile.js"); const outputFil ...

  3. windows下基于(QPC)实现的微秒级延时

    1.为什么会写windows下微秒级延时 在上一篇 实现memcpy()函数及过程总结 中测试memcpy的效率中,测试时间的拷贝效率在微秒级别,需要使用微秒级时间间隔计数. windows下提供Qu ...

  4. Java集合和数组的比较(为什么引入集合)

    数组不是面向对象的,存在明显的缺陷,集合完全弥补了数组的一些缺点,比数组更灵活更实用,可大大提高软件的开发效率而且不同的集合框架类可适用于不同场合.具体如下: 1)数组的效率高于集合类. 2)数组能存 ...

  5. Java集合体系结构(List、Set、Collection、Map的区别和联系)

    Java集合体系结构(List.Set.Collection.Map的区别和联系) 1.Collection 接口存储一组不唯一,无序的对象 2.List 接口存储一组不唯一,有序(插入顺序)的对象 ...

  6. oracle 中||

    oracle里双竖线是字符串连接运算符!

  7. 使用KEIL C51实现的简单合作式多任务操作系统内核(单片机实现版本)

    基于网上网友的代码,自己在单片机上实现, 特此记录分享之. 基于https://blog.csdn.net/yyx112358/article/details/78877523 //使用KEIL C5 ...

  8. mysql 查询表的最大时间 的数据

    SELECT * from (SELECT MAX(a.update_date) as q ,a.monitoring_point_id from biz_monitoring_point_recor ...

  9. AcWing 228. 异或 (dfs+线性基)打卡

    题目:https://www.acwing.com/problem/content/230/ 题意:有一个图,每条边有一个权值,现在求1-n的一条路径的最大异或和,一条边能经过多次,相应的也要计算那么 ...

  10. spring MVC 返回值信息和ResponseBody的响应json数据

    spring mvc的界面返回: 如果我们定义的返回类型是String 那么我们返回的时候直接写入 我们的界面的名字就可以了  springmvc会自动去找到我们的界面,如果是void类型的返回那么 ...