Spring+mybatis整合

整合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 List<Category>list();
 public  int count();
}

6.编写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.zgf.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="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>

···
7.编写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: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">

<context:annotation-config />


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://localhost:3306/how2java?useUnicode=true&amp;characterEncoding=UTF-8</value>

    </property>
    <property name="username">
        <value>root</value>
    </property>
    <property name="password">
        <value>mysql123</value>
    </property>
</bean>

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

</beans>
···

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<Category> categoryList=categoryMapper.list();
    for(Category category :categoryList){
        System.out.println(category.getName());
    }
}

}
···
下面是结构目录


捕获.PNG
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容