Spring Boot整合MyBatis

需求分析

通过使用 SpringBoot+MyBatis整合实现一个对数据库中的 users 表的 CRUD

创建工程

04_springboot_mybatis

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
    
    
    com.by
    04_springboot_mybatis
    1.0-SNAPSHOT
    
        1.8
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
        
            com.alibaba
            druid
            1.0.9
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.apache.commons
            commons-lang3
            3.0
        
    

 

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3305/springboot
    username: root
    password:
    type: com.alibaba.druid.pool.DruidDataSource
logging:
  level:
    com:
      by:
        mapper: DEBUG

启动类

@SpringBootApplication
@MapperScan("com.by.mapper") // @MapperScan 用户扫描MyBatis的Mapper接口
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

添加用户

数据表设计

    CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `nam` varchar(255) DEFAULT NULL,
      `sex` int(11) DEFAULT NULL,
      `pwd` varchar(255) DEFAULT NULL,
      `birth` datetime DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

pojo

public class User {
    private Integer id;

    private String nam;

    private String pwd;
    
    private Integer sex;

    private Date birth;
//getter和setter方法省略
}

mapper

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



    
		insert into user(nam,pwd,sex,birth) values(#{nam},#{pwd},#{sex},#{birth})
	

service

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;

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

junit

/**
 *  main方法:
 *		ApplicationContext ac=new 
 *       			ClassPathXmlApplicationContext("classpath:applicationContext.xml");
 *  junit与spring整合:
 *      @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
 *   	@Contextconfiguartion("classpath:applicationContext.xml")  
 *  junit与SpringBoot整合: 
 *		@RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
 *		@SpringBootTest(classes={App.class}):加载SpringBoot启动类。启动springBoot
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App.class})
public class UserServiceTest {

	@Autowired
	private UserService userService;
	
	@Test
	public void testAddUser(){
		User user = new User();
		user.setId(1);
		user.setNam("二狗");
		user.setPwd("111");
        user.setSex(1);
		user.setBirth(new Date());
		this.userService.addUser(user);
	}
}

你可能感兴趣的:(spring,boot,mybatis,后端)