Spring整合MyBatis

1.创建工程

Spring整合MyBatis_第1张图片

1.1pom.xml  



    
        Spring
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    spring_mybatis

    
        8
        8
    
    
        
        
            org.springframework
            spring-context
            5.1.8.RELEASE
        
        
            org.springframework
            spring-aspects
            5.1.8.RELEASE
        
        
            org.springframework
            spring-tx
            5.1.8.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.1.8.RELEASE
        
        
        
            mysql
            mysql-connector-java
            8.0.25
        
        
        
            com.alibaba
            druid
            1.1.0
        
        
        
            org.mybatis
            mybatis
            3.5.3
        
        
            org.mybatis
            mybatis-spring
            2.0.3
        
        
        
            org.slf4j
            slf4j-log4j12
            1.7.26
        
        
        
            org.springframework
            spring-test
            5.1.8.RELEASE
        
        
            junit
            junit
            4.12
        
    

    
        
            
                src/main/java
                
                    **/*.xml
                
            
        
    

 Spring整合MyBatis_第2张图片

Spring整合MyBatis_第3张图片 1.2log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# 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

 1.3applicationContext.xml



    
    

 2.配置数据源

2.1db.properties

这里是navicat版本5.0以上的配置

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=1111

这里是navicat版本8.0以上的配置

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?useSSL=false&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456

2.2applicationContext.xml




    
    
    
    
    
        
        
        
        
     

 3.整合MyBatis

3.1applicationContext.xml

  
    
        
    

    
    
        
        
        
    

 4.测试

 4.1创建spring表

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `money` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

 4.2pojo

package com.by.pojo;

public class User {
    private Integer id;
    private String name;
    private Float money;

    public User(String name, Float money) {
        this.name = name;
        this.money = money;
    }

    public User() {
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }
}

 4.3mapper

public interface UserMapper {
    public void addUser(User user);
}

 mapper.xml




    
        insert into t_user(name,money) values(#{name},#{money})
    

4.4service

package com.by.service;

import com.by.mapper.UserMapper;
import com.by.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(User user){
        userMapper.addUser(user);
    }
}

 4.5junit

package com.by.web;

import com.by.pojo.User;
import com.by.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)//加载配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {

@Autowired
    private UserService userService;

@Test
    public void addUser(){
   userService.addUser(new User("小龙女",4000F));
   userService.addUser(new User ("李莫愁",2000F));
}

}

 4.6结果

Spring整合MyBatis_第4张图片

对应数据库中的结果

 

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