Mybatis+Spring的整合练习

一、建立数据库、建表

二、新建maven项目

三、添加依赖


    
      junit
      junit
      3.8.1
      test
    
    
    
        org.mybatis
        mybatis
        3.5.0
    
    
     
            org.mybatis
            mybatis-spring
            1.3.0
      
    
    
            mysql
        mysql-connector-java
        8.0.13
    
    
    
org.springframework spring-core 4.3.2.RELEASE org.springframework spring-context 4.3.2.RELEASE org.springframework spring-test 4.3.2.RELEASE org.springframework spring-tx 4.3.2.RELEASE org.springframework spring-beans 4.3.2.RELEASE org.springframework spring-jdbc 4.3.2.RELEASE
com.mchange c3p0 0.9.5.2 commons-io commons-io 2.4

四、建立包,搭建项目架构

   Mybatis+Spring的整合练习_第1张图片

五、根据数据库写对应的pojo类

package com.dongtian.MyBatis_Spring.pojo;

import java.util.Date;

public class User {
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
                + address + "]";
    }    
}

六、写映射器和接口

package com.dongtian.MyBatis_Spring.mapper;

import org.springframework.stereotype.Repository;

import com.dongtian.MyBatis_Spring.pojo.User;
@Repository
public interface UserMapper {
    public void insertUser(User user);
}

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


    
        insert into user(username,sex,address)
            values(#{username},#{sex},#{address})
        

七、写业务层接口和接口实现类

package com.dongtian.MyBatis_Spring.service;

import com.dongtian.MyBatis_Spring.pojo.User;

public interface UserService {
    public void insertUser(User user);
}
package com.dongtian.MyBatis_Spring.serviceImp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dongtian.MyBatis_Spring.mapper.UserMapper;
import com.dongtian.MyBatis_Spring.pojo.User;
import com.dongtian.MyBatis_Spring.service.UserService;

@Service
public class UserServiceImp implements UserService{
    @Autowired
    private UserMapper userMapper = null;
    
    @Transactional(propagation = Propagation.REQUIRES_NEW,
            isolation=Isolation.READ_COMMITTED
            )
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }

}

八、简单测试

package com.dongtian.MyBatis_Spring;

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

import com.dongtian.MyBatis_Spring.pojo.User;
import com.dongtian.MyBatis_Spring.service.UserService;

public class TestMyDemo {
    public static void main(String[] args) {
        //加载配置文件
        ApplicationContext applicationContext = 
                    new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理对象
        UserService userService = applicationContext.getBean(UserService.class);
        //通过接口对象调用接口方法
        User user = new User();
        user.setUsername("王力宏");
        user.setSex("男");
        user.setAddress("上海");
        userService.insertUser(user);
        System.out.println(user);
    }
}

九、流程逻辑比较简单:

  通过拿到业务层接口的代理对象,代理对象调用接口中的方法,接口方法调用映射器中的方法,由mapper操作数据库。

十、重点在配置文件中

(1)sqlMapConfig.xml : 大部分事情都交给了spring


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

    
        <package name="com.dongtian.MyBatis_Spring.mapper"/>        
    

(2)applicationContext.xml


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
    
    
    
    package="com.dongtian.MyBatis_Spring.*"/>
    
    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        
        
        
    

    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
    
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
        
    
    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
    
    
    

 

转载于:https://www.cnblogs.com/dongtian-blogs/p/10821689.html

你可能感兴趣的:(java,数据库,测试)