Spring mvc 搭建Mybatis
本文建立在spring mvc已经搭建起来的基础上。
首先看要引入的jar包,其中高亮的是为了mybatis新引入的。
- <properties>
- <spring.webmvc.version>4.3.0.RELEASE</spring.webmvc.version>
- <junit.version>4.8.2</junit.version>
- <jedis.version>2.8.1</jedis.version>
- <log4j.version>1.2.16</log4j.version>
- <slf4j.version>1.7.2</slf4j.version>
- <spring.data.redis.version>1.7.2.RELEASE</spring.data.redis.version>
- <spring.data.mongodb.version>1.8.0.RELEASE</spring.data.mongodb.version>
- <mybatis.version>3.2.0</mybatis.version>
- <dbcp.version>1.2.2</dbcp.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.webmvc.version}</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>${jedis.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>${spring.data.redis.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-mongodb</artifactId>
- <version>${spring.data.mongodb.version}</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.30</version>
- </dependency>
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>${dbcp.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.webmvc.version}</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>2.4.3</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.4.3</version>
- </dependency>
- </dependencies>
jdbc.properties:
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://192.168.1.101:3306/test
- username=root
- password=root
- #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
- initialSize=0
- #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
- maxActive=20
- #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
- maxIdle=20
- #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
- minIdle=1
- #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
- maxWait=60000
mybatis.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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache-4.2.xsd ">
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- <!-- 初始化连接大小 -->
- <property name="initialSize" value="${initialSize}"></property>
- <!-- 连接池最大数量 -->
- <property name="maxActive" value="${maxActive}"></property>
- <!-- 连接池最大空闲 -->
- <property name="maxIdle" value="${maxIdle}"></property>
- <!-- 连接池最小空闲 -->
- <property name="minIdle" value="${minIdle}"></property>
- <!-- 获取连接最大等待时间 -->
- <property name="maxWait" value="${maxWait}"></property>
- </bean>
- <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <!-- 自动扫描mapping.xml文件 -->
- <property name="mapperLocations" value="classpath:com/zjf/**/mybatis_*.xml"></property>
- </bean>
- <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.zjf" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
- </bean>
- <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- </beans>
java代码目录结构:

注意:这里没有dao的impl,因为Mybatis会根据mapper.xml去创建一个impl。也就是说,mapper.xml就一个一个impl。
PersonController.java:
- package com.zjf.spring.mybatis;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.zjf.spring.mybatis.model.Person;
- import com.zjf.spring.mybatis.service.IPersonService;
- @Controller
- @RequestMapping("person")
- public class PersonController {
- @Autowired
- private IPersonService personService;
- @RequestMapping(value="/get/{id}")
- public @ResponseBody Person get(@PathVariable Integer id)
- {
- Person person = personService.getPersonById(id);
- return person;
- }
- }
PersonServiceImpl.java:
- package com.zjf.spring.mybatis.service.impl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.zjf.spring.mybatis.dao.IPersonDao;
- import com.zjf.spring.mybatis.model.Person;
- import com.zjf.spring.mybatis.service.IPersonService;
- @Service(value="personService")
- public class PersonServiceImpl implements IPersonService {
- @Autowired
- private IPersonDao personDao;
- public Person getPersonById(Integer id) {
- // TODO Auto-generated method stub
- return personDao.selectByPrimaryKey(id);
- }
- }
IPersonDao.java
- package com.zjf.spring.mybatis.dao;
- import com.zjf.spring.mybatis.model.Person;
- public interface IPersonDao {
- int deleteByPrimaryKey(Integer id);
- int insert(Person record);
- int insertSelective(Person record);
- Person selectByPrimaryKey(Integer id);
- int updateByPrimaryKeySelective(Person record);
- int updateByPrimaryKey(Person record);
- }
Person.java:
- package com.zjf.spring.mybatis.model;
- public class Person {
- private Integer id;
- private String name;
- private Integer age;
- private String address;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name == null ? null : name.trim();
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address == null ? null : address.trim();
- }
- }
mybatis_PersonMapper.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.zjf.spring.mybatis.dao.IPersonDao" >
- <resultMap id="BaseResultMap" type="com.zjf.spring.mybatis.model.Person" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- <result column="age" property="age" jdbcType="INTEGER" />
- <result column="address" property="address" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, name, age, address
- </sql>
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_person
- where id = #{id,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_person
- where id = #{id,jdbcType=INTEGER}
- </delete>
- <insert id="insert" parameterType="com.zjf.spring.mybatis.model.Person" >
- insert into t_person (id, name, age,
- address)
- values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
- #{address,jdbcType=VARCHAR})
- </insert>
- <insert id="insertSelective" parameterType="com.zjf.spring.mybatis.model.Person" >
- insert into t_person
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- id,
- </if>
- <if test="name != null" >
- name,
- </if>
- <if test="age != null" >
- age,
- </if>
- <if test="address != null" >
- address,
- </if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- #{id,jdbcType=INTEGER},
- </if>
- <if test="name != null" >
- #{name,jdbcType=VARCHAR},
- </if>
- <if test="age != null" >
- #{age,jdbcType=INTEGER},
- </if>
- <if test="address != null" >
- #{address,jdbcType=VARCHAR},
- </if>
- </trim>
- </insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.zjf.spring.mybatis.model.Person" >
- update t_person
- <set >
- <if test="name != null" >
- name = #{name,jdbcType=VARCHAR},
- </if>
- <if test="age != null" >
- age = #{age,jdbcType=INTEGER},
- </if>
- <if test="address != null" >
- address = #{address,jdbcType=VARCHAR},
- </if>
- </set>
- where id = #{id,jdbcType=INTEGER}
- </update>
- <update id="updateByPrimaryKey" parameterType="com.zjf.spring.mybatis.model.Person" >
- update t_person
- set name = #{name,jdbcType=VARCHAR},
- age = #{age,jdbcType=INTEGER},
- address = #{address,jdbcType=VARCHAR}
- where id = #{id,jdbcType=INTEGER}
- </update>
- </mapper>
注:mybatis_PersonMapper.xml中的配置将会映射成它配置的namespace接口的实现,然后每个节点(update select insert)都对应一个接口的方法。名字要与接口的方法名字一致。
最终效果:

Spring mvc 搭建Mybatis的更多相关文章
- 搭建Spring、Spring MVC、Mybatis和Freemarker
搭建Spring.Spring MVC.Mybatis和Freemarker 1.pom文件 <project xmlns="http://maven.apache.org/POM/4 ...
- spring MVC、mybatis配置读写分离
spring MVC.mybatis配置读写分离 1.环境: 3台数据库机器,一个master,二台slave,分别为slave1,slave2 2.要实现的目标: ①使数据写入到master ②读数 ...
- spring mvc与mybatis收集到博客
mybaits-spring 官方教程 http://mybatis.github.io/spring/zh/ SpringMVC 基础教程 框架分析 http://blog.csdn.net/swi ...
- Spring Mvc和Mybatis的多数据库访问配置过程
Spring Mvc 加Mybatis的多数据库访问源配置访问过程如下: 在applicationContext.xml进行配置 <?xml version="1.0" en ...
- Spring、Spring MVC、MyBatis
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Sp ...
- IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架
项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
- Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)
Spring.Spring MVC.MyBatis整合(未万待续)
随机推荐
- flutter routes跳转
flutter可以通过push pop跳转到上一级或者下一级 基本push跳转方法 此时仍然有返回按钮 Navigator.push( context, MaterialPageRoute( buil ...
- Python_List对象内置方法详解
目录 目录 前言 软件环境 列表List 修改列表的元素 插入列表元素 extend 将序列中的元素迭代的附加到list中 insert 在指定的索引号中插入一个元素 删除列表元素 del 删除Lis ...
- Selenium 2自动化测试实战4(引用模块)
一.模组1.模组也叫类库或模块,引用模块 在python中,通过import….或from….import….的方式引用模块,下面引用time模块 import time print (time.ct ...
- CentOS 7 Docker 安装
CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 本文以 CentOS 7.6 版本 ...
- PyCharm给函数增加文档注释
选择函数名,左上角会出现一个小灯泡,点击小灯泡 选择第二项 选中调用的函数名 Ctrl + Q 显示注释 如何配置操作习惯 File > sitting > 搜索 'keymap' > ...
- 根据输入的整数n使得输出精确到小数n位
#include<iostream> #include<stdio.h> using namespace std; int main(){ int a,b,c; while(t ...
- cocos2dx基础篇(25) 简单碰撞检测
[3.x] 将数学类 CCPoint.CCRect 改为v3.x版本的 Vec2.Rect 就好了. [简单碰撞检测] 在一些游戏中经常会遇到碰撞检测的情况,如愤怒的小鸟飞出去后,是否与石头发生碰撞. ...
- typedef interrupt void (*PINT)(void)的分析
今天写程序时,在DSP2833x_PieVect.h看到typedef interrupt void (*PINT)(void)突然一愣,上网查了下发现在这是加了interrupt 中断关键字的函数指 ...
- 【嵌入式开发】树莓派+官方摄像头模块+VLC串流实时输出网络视频流
sudo apt-get update sudo apt-get install vlc sudo raspivid -o - -t 0 -w 640 -h 360 -fps 25|cvlc -vvv ...
- The system has no LUN copy license
[问题描述] ipsan作为cinder后端的时候,通过快照创建磁盘失败,报以下错误: { u 'data': {}, u 'error': { u 'code': 1077950181, u 'de ...