SpringBoot——Mybatis-xml(十四)
依赖
1 2 3 4 5
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
|
配置
1 2 3
| mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.qn.eneity
|
实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package com.qn.eneity;
public class User { private Integer id;
private String userName;
private String passWord;
private String realName;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getPassWord() { return passWord; }
public void setPassWord(String passWord) { this.passWord = passWord; }
public String getRealName() { return realName; }
public void setRealName(String realName) { this.realName = realName; }
@Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", passWord='" + passWord + '\'' + ", realName='" + realName + '\'' + '}'; } }
|
Mapper.xml
1 2 3 4 5 6 7 8 9 10 11
| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qn.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.qn.eneity.User"> <result column="id" jdbcType="INTEGER" property="id"/> <result column="userName" jdbcType="VARCHAR" property="userName"/> <result column="passWord" jdbcType="VARCHAR" property="passWord"/> <result column="realName" jdbcType="VARCHAR" property="realName"/> </resultMap> <select id="Sel" resultMap="BaseResultMap"> select * from user where id = #{id} </select> </mapper>
|
Mapper映射类
1 2 3 4 5 6 7 8 9
| package com.qn.mapper;
import com.qn.eneity.User; import org.springframework.stereotype.Repository;
@Repository public interface UserMapper { User Sel(int id); }
|
业务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.qn.service;
import com.qn.eneity.User; import com.qn.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
@Service public class UserService { @Autowired UserMapper userMapper;
public User Sel(int id) {
return userMapper.Sel(id);
} }
|
添加扫描
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.qn;
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean;
import java.util.Arrays;
@MapperScan("com.qn.mapper") @SpringBootApplication @ServletComponentScan public class SpringbootDemoApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootDemoApplication.class, args); }
@Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }; } }
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.qn.service;
import com.qn.eneity.User; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest @RunWith(SpringRunner.class) class UserServiceTest {
@Autowired UserService userService;
@Test void sel() { User user = userService.Sel(2); System.out.println(user.toString()); } }
|