java之mybatis整合spring

这篇讲解spring+mybatis的整合。

目录结构:

java之mybatis整合spring_第1张图片

一. 整合spring的第一种方法

1. 新建 java 项目 : spring_mybatis

2.导入jar 包-----spring和mybatis的整合包,然后build

aopalliance.jar
aspectjweaver.jar
commons-logging.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.20-bin.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar

3.编写 vo 类

在cn.vincent.vo下 User.java

 1 package cn.vincent.vo;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6 
 7     private int id;
 8     private String name;
 9     private int age;
10     private int rileId;
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public int getAge() {
24         return age;
25     }
26     public void setAge(int age) {
27         this.age = age;
28     }
29     public int getRileId() {
30         return rileId;
31     }
32     public void setRileId(int rileId) {
33         this.rileId = rileId;
34     }
35     @Override
36     public String toString() {
37         return "User [id=" + id + ", name=" + name + ", age=" + age
38                 + ", rileId=" + rileId + "]";
39     }
40     
41     
42 }
View Code

4.编写 映射文件

在cn.vincent.mapper下 UserMapper.xml


DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    

5.编写 dao

在cn.vincent.mapper下 UserMapper.java

package cn.vincent.mapper;

import java.util.List;

import cn.vincent.vo.User;

public interface UserMapper {

    public List findAll();
}

在cn.vincent.mapper.impl下 UserMapperImpl.java

package cn.vincent.mapper.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.vincent.mapper.UserMapper;
import cn.vincent.vo.User;

@Repository("userMapper")
public class UserMapperImpl implements UserMapper {

    @Autowired
    private SqlSession sqlSession;
    public void setSqlSession(SqlSession sqlSession){
        this.sqlSession=sqlSession;
    }
    
    @Override
    public List findAll() {
        return sqlSession.selectList("cn.vincent.mapper.UserMapper.findAll");
    }

    
}

6.编写 service

在 cn.vincent.service下 UserService.java

package cn.vincent.service;

import java.util.List;

import cn.vincent.vo.User;

public interface UserService {

    public List findAll();
}

在 cn.vincent.service下 UserServiceImpl.java

package cn.vincent.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.vincent.mapper.UserMapper;
import cn.vincent.service.UserService;
import cn.vincent.vo.User;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    
    
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    @Override
    public List findAll() {
        return userMapper.findAll();
    }

    
}

7. 编写mybatis的配置文件

mybatis.cfg.xml


DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

    
        <package name="cn.vincent.vo"/>
    
    
        UserMapper.xml"/>
    

8. 编写spring的配置文件

beans.xml



    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        location" value="classpath:jdbc.properties">
    
    dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          
          
          
          
      
    
    sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        dataSource">
        mybatis.cfg.xml"/>
    
    sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        sqlSessionFactory">
    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
    
    
        
          
          
          
          
          
      
    
    
        
        expression="execution(* cn.vincent.service.impl.*.*(..))" id="pointcut"/>
        
     
    package="cn.vincent">
 

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

9.添加测试

在 test下的 cn.vincent.service下的 UserServiceTest.java

package cn.vincent.service;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.vincent.vo.User;

public class UserServiceTest {

    @Test
    public void testFindAll(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        UserService userService=ac.getBean(UserService.class);
        List list=userService.findAll();
        for(User u:list){
            System.out.println(u);
        }
    }
}

10. 运行测试

效果如下:

java之mybatis整合spring_第2张图片

二. 第二种是去掉mybatis配置文件的配置方法

在beans.xml中修改 SqlSessionFactory的配置

    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        typeAliasesPackage" value="cn.vincent.vo">
        
            
                classpath:cn/vincent/mapper/UserMapper.xml
            
        
    

三. 可以通过代理的方式来生成实现类的配置

beans.xml



    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
      dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          
          
          
          
      
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        typeAliasesPackage" value="cn.sxt.vo">
        mapperLocations">
            
                classpath:cn/sxt/mapper/UserMapper.xml
            
        
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
    
    
        
          
          
          
          
          
      
    
    
        
        
        
     
       package="cn.vincent">

github地址:https://github.com/Vincent-yuan/spring_mybatis

你可能感兴趣的:(java之mybatis整合spring)