说明: mybatis简单配置使用
1.pom文件修改:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>berg.study.hello</groupId>
<artifactId>hello-spring-boot</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-spring-boot</name>
<description>add jdbc</description>
<!-- lookup parent from repository -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
<scope>runtime</scope>
</dependency>
<!--mybatis 使用-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建配置文件类:
@Configuration
@MapperScan("berg.study.hello.boot.mysql.mybatis.mapper")
public class MybatisConfig {
@Resource
private DataSource dataSource;
/**
* 让spring 容器管理
* @return
*/
@Bean
public SqlSessionFactoryBean getSessionFactory(){
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
}
3.创建映射类:
@Repository
public interface TeacherInfoMapper {
@Select("SELECT * FROM teacher_info WHERE teacherId=#{teacherId}")
TeacherInfo getById(Long teacherId);
@Results({
@Result(property = "userId", column = "id"),
@Result(property = "userName", column = "name"),
@Result(property = "onlineState", column = "online_state"),
@Result(property = "userState", column = "user_status")
})
@Select("SELECT * FROM test_user WHERE id=#{userId}")
UserInfo getUserById(Long userId);
}
4.创建model类:
public class TeacherInfo implements Serializable{
private Long teacherId;
private Long userId;
private String loginName;
private String teacherName;
private String teacherHeadImg;
private String teacherVideo;
private String organization;
get,set---
5.新建service:
public interface TeacherInfoService {
/**
* 获得单一老师信息,通过id
* @param teacherId
* @return
*/
TeacherInfo getTeacherInfoById(Long teacherId);
}
@Service
public class TeacherInfoServiceImpl implements TeacherInfoService {
@Resource
private TeacherInfoMapper teacherInfoMapper;
@Override
public TeacherInfo getTeacherInfoById(Long teacherId) {
return teacherInfoMapper.getById(teacherId);
}
}
6.创建测试用例:
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloSpringBootApplicationTests {
@Resource
private TeacherInfoService teacherInfoService;
@Test
public void testMybatis(){
final Long teacherId=3L;
TeacherInfo teacherInfo=teacherInfoService.getTeacherInfoById(teacherId);
Assert.assertNotNull(teacherInfo);
Assert.assertEquals("丫丫",teacherInfo.getTeacherName());
System.out.println("=======ok========="+teacherInfo);
}
}