整合Spring+mybatis的基本思路是把SessionFactory交给Spring管理。
1.建立数据库
···
/*
Navicat MySQL Data Transfer
Source Server : mvc_user
Source Server Version : 50637
Source Host : localhost:3306
Source Database : how2java
Target Server Type : MYSQL
Target Server Version : 50637
File Encoding : 65001
Date: 2018-03-11 13:48:20
*/
SET FOREIGN_KEY_CHECKS=0;
-- Table structure for category_
DROP TABLE IF EXISTS category_
;
CREATE TABLE category_
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(32) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
-- Records of category_
INSERT INTO category_
VALUES ('15', '张三');
INSERT INTO category_
VALUES ('13', 'new Category');
INSERT INTO category_
VALUES ('14', 'new Category');
2.用idea建立工程
3.导入jar包
4.编写pojo
Category.java
package com.zgf.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;
}
@Override
public String toString(){
return "Category [id="+id+",name="+name+"]";
}
}
5.写mapper
package com.zgf.mapper;
import com.zgf.pojo.Category;
import java.util.List;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public Category get(int id);
public int update(Category category);
public Listlist();
public int count();
}
6.编写Category.xml
···
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into category_ (name) VALUES (#{name})
delete from category_ where id=#{id}
update category_ set name=#{name} where id=#{id}
···
7.编写applicationContext.xml
···
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/how2java?useUnicode=true&characterEncoding=UTF-8
root
mysql123
···
8.导入log4j.properties
···
Global logging configuration
log4j.rootLogger=ERROR, stdout
MyBatis logging configuration...
log4j.logger.com.how2java=TRACE
Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
9.编写测试类
package com.java.test;
import com.zgf.mapper.CategoryMapper;
import com.zgf.pojo.Category;
import org.junit.Test;
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 java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private CategoryMapper categoryMapper;
@Test
public void testAdd(){
Category category=new Category();
category.setName("张三");
categoryMapper.add(category);
}
@Test
public void delete(){
Category category=new Category();
categoryMapper.delete(2);
}
@Test
public void list(){
System.out.println(categoryMapper);
List categoryList=categoryMapper.list();
for(Category category :categoryList){
System.out.println(category.getName());
}
}
}
···
下面是结构目录