Mybatis 和 Spring 4 全注解配置

废话不说直接上代码

  1. 注解配置文件:

Application.java

package com.test.config;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.test.config.Application;

@Configuration
@EnableTransactionManagement
public class Application {
    @Bean
    public  DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("postgres");
        dataSource.setPassword("postgres");
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql:postgres");
        return dataSource;
    }

    @Bean 
    MapperScannerConfigurer mpperScannnerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
        msc.setBasePackage("com.test.mapper");
        return msc;
    }


    @Bean(name="sqlSessionFactory")
    SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setDataSource(dataSource);
        ssfb.setTypeAliasesPackage("com.test.entity");
        return ssfb;
    }
    @Bean
    PlatformTransactionManager transactionManager (DataSource dataSource){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  1. UserMapper 接口
package com.test.mapper;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.test.entity.User;

public interface UserMapper {
    void save(User user);
    void update (User user);
    int delete (int id);
    User findById(int id);
    User findByName(String name);
    List findAll();
}
  1. UserMapper.xml



<mapper namespace="com.test.mapper.UserMapper">

  <insert id="save" parameterType="User">
    insert into t_user(user_name,user_age) values(#{name},#{age})
  insert>

  <update id="update" parameterType="User">
    update t_user set user_name=#{name},user_age=#{age} where user_id=#{id}
  update>

  <delete id="delete" parameterType="int">
    delete from t_user where user_id=#{id}
  delete>

     
  <select id="findById" parameterType="int" resultType="User">
    select user_id as id,user_name as name,user_age as age from t_user where user_id=#{id}
  select>
  <select id="findByName" parameterType="String" resultType="User">
    select user_id as id,user_name as name,user_age as age from t_user where user_name=#{name}
  select>
  <select id="findAll" resultType="User">
    select user_id as id,user_name as name,user_age as age from t_user
  select>

mapper>
  1. 测试代码
package com.test.mapper;


import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.test.config.Application;
import com.test.entity.User;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
@TransactionConfiguration(defaultRollback=true)
public class UserMapperTest {

    @Autowired()
    private UserMapper userDao;

    @Test
    public void findAllUsers(){
        List userLists = userDao.findAll();
        Assert.assertEquals(1, userLists.size());
        Assert.assertEquals(1, userLists.get(0).getId());
        Assert.assertEquals("liuyan", userLists.get(0).getName());
        Assert.assertEquals(26, userLists.get(0).getAge());
    }

    @Test
    @Transactional
    public void insertUser(){
        User user = new User();
        user.setAge(30);
        user.setName("tester");
        user.setPassword("password");
        List userListsSaveBefore  = userDao.findAll();
        Assert.assertEquals(1, userListsSaveBefore.size());
        userDao.save(user);
        List userListsSaveAfter  = userDao.findAll();
        Assert.assertEquals(2, userListsSaveAfter.size());

    }

}
  1. pom.xml

<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>

    <parent>
        <groupId>com.testgroupId>
        <artifactId>SpringMybatisTestartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <artifactId>modelartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.3.0version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.2version>
        dependency>
        <dependency>
            <groupId>org.postgresqlgroupId>
            <artifactId>postgresqlartifactId>
            <version>9.4-1201-jdbc41version>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>testscope>
        dependency>
    dependencies>

project>

你可能感兴趣的:(mybatis,spring4,mybatis,spring4)