spring整合mybatis基于注解

  • 数据库

/*

Navicat MySQL Data Transfer

 

Source Server         : mysql

Source Server Version : 50549

Source Host           : localhost:3306

Source Database       : ooxx

 

Target Server Type    : MYSQL

Target Server Version : 50549

File Encoding         : 65001

 

Date: 2019-09-19 13:23:49

*/

 

SET FOREIGN_KEY_CHECKS=0;

 

-- ----------------------------

-- Table structure for user

-- ----------------------------

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(255) DEFAULT NULL,

  `pwd` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

 

-- ----------------------------

-- Records of user

-- ----------------------------

INSERT INTO `user` VALUES ('1', '康康', '56345');

INSERT INTO `user` VALUES ('2', '乔治大哥', '123456');

INSERT INTO `user` VALUES ('3', 'George', '123456');

INSERT INTO `user` VALUES ('4', '董小康', '123456');

 

  • Pom.xml

xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.henugroupId>
    <artifactId>spring_mybatis_xmlartifactId>
    <version>1.0-SNAPSHOTversion>

    
    <dependencies>
        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjrtartifactId>
            <version>1.6.11version>
        dependency>
        
        
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.3version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.5version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.2version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-expressionartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>compilescope>
        dependency>
        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>5.1.2version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.0version>
        dependency>

    dependencies>

project>

 

  • Bean

package com.henu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author George
 * @description
 **/
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
    private Integer id;
    private String name;
    private String pwd;
}

 

  • Dao

package com.henu.dao;

import com.henu.bean.User;

import java.util.List;

/**
 * @author George
 * @description
 **/
public interface UserDao {
    List queryAll();
    int insert(User user);
}

 

  • Service

package com.henu.service;

import com.henu.bean.User;

import java.util.List;

public interface UserService {
    List queryAll();
    int insert(User user);
}

 

  • serviceImpl

package com.henu.service;

import com.henu.bean.User;
import com.henu.dao.UserDao;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author George
 * @description
 **/
@Data
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public List queryAll() {
        List users = userDao.queryAll();
        return users;
    }
    @Transactional
    public int insert(User user) {
        return userDao.insert(user);
    }
}

  • Resources_db.properties

#db config
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ooxx
jdbc.username=root
jdbc.password=123456
initialSize=5
minIdle=2
maxActive=10
maxWait=10000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
testWhileIdle=true
testOnBorrow=true
testOnReturn=false
poolPreparedStatements=true
maxPoolPreparedStatementPerConnectionSize=20
defaultAutoCommit=true
validationQuery=select 1

 

  • Resources_sqlMybatisConfig.xml

xml version="1.0" encoding="UTF-8"?>
 configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        
        <typeAlias type="com.henu.bean.User" alias="user">typeAlias>
    typeAliases>
    
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            
            <property name="reasonable" value="true">property>
        plugin>
    plugins>

configuration>

 

  • Resources_springContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <context:property-placeholder location="classpath:db.properties">context:property-placeholder>

    
    <context:component-scan base-package="com.henu">context:component-scan>


    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        
        
        <property name="initialSize" value="${initialSize}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxActive" value="${maxActive}"/>
        
        <property name="maxWait" value="${maxWait}"/>
        
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
        
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>
        <property name="testWhileIdle" value="${testWhileIdle}"/>
        
        <property name="testOnBorrow" value="${testOnBorrow}"/>
        <property name="testOnReturn" value="${testOnReturn}"/>
        
        <property name="poolPreparedStatements" value="${poolPreparedStatements}"/>
        <property name="maxPoolPreparedStatementPerConnectionSize"
                  value="${maxPoolPreparedStatementPerConnectionSize}"/>
        
        <property name="defaultAutoCommit" value="${defaultAutoCommit}"/>
        
        <property name="validationQuery" value="${validationQuery}"/>
    bean>

    
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="configLocation" value="classpath:sqlMybatisConfig.xml">property>
        
        <property name="mapperLocations" value="classpath:mapper/UserDao.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.henu.dao">property>
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>


    
    
    <bean name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

    
    <tx:annotation-driven transaction-manager="txManager">tx:annotation-driven>

beans>

  • Resources_UserDao.xml

xml version="1.0" encoding="UTF-8"?>
 mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.henu.dao.UserDao">

    <resultMap id="userMap" type="User">
        <id property="id" column="id">id>
        <result property="name" column="name">result>
        <result property="pwd" column="pwd">result>
    resultMap>


    <select id="queryAll" resultMap="userMap">
        select * from user;
    select>

    <insert id="insert" parameterType="user">
        insert into user values(#{id},#{name},#{pwd})
    insert>

mapper>

 

  • 测试SMTest

package com.henu.text;

import com.henu.bean.User;
import com.henu.service.UserService;
import lombok.AllArgsConstructor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @author George
 * @description
 **/
public class SMTest {
    private ClassPathXmlApplicationContext context = null;
    private UserService userService = null;

    @Before
    public void init(){
        context = new ClassPathXmlApplicationContext("springContext.xml");
        userService = context.getBean(UserService.class);
    }

    @Test
    public void queryAll(){
        List users = userService.queryAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void insert(){
        User user = new User(null,"董小康","123456");
        userService.insert(user);
    }

    @After
    public void destory(){
        context.close();
    }
}

 

 

你可能感兴趣的:(#,spring)