本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid。SpringBoot主要有两种方式来实现。一种是用xml文件;一种不用xml,使用注解。两种方式各有优异,具体使用视自身而定。
项目结构如下
首先需要引入依赖有:
pom.xml
<!--添加druid连接池依赖,手动添加-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!--添加mybatis依赖,在新建项目时可勾选-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--添加MySQL依赖,在新建项目时可勾选-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--添加lombok依赖,需要下载插件后,在新建项目时可勾选,-->
<!--使用Lombok主要是为了减少代码,具体用法自行百度-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
entity层
package com.springboot.chapter4.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.Alias;
/**
* Created on 2019/8/15 by Tinchi
**/
@Data //lombok的强大,该注解可以省去getter和setter
@AllArgsConstructor //全参数的构造函数
@NoArgsConstructor //无参数的构造函数
@Alias(value = "user") // MyBatis 指定别名
public class User {
private Long id;
private String userName;
private String note;
}
controller层
UserController.java
package com.springboot.chapter4.controller;
import com.springboot.chapter4.entity.User;
import com.springboot.chapter4.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created on 2019/8/15 by Tinchi
**/
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
//1、不使用xml文件
@GetMapping(value = "/getAllUser")
public List<User> getAllUser(){
return userService.getAllUser();
}
//2、使用xml文件
@GetMapping(value = "/getUser")
public List<User> getUser(){
return userService.getUser();
}
}
service层
UserService.java
package com.springboot.chapter4.service;
import com.springboot.chapter4.entity.User;
import java.util.List;
/**
* Created on 2019/8/15 by Tinchi
**/
public interface UserService {
//实现获取所有user的两种方式
public List<User> getAllUser(); //1、不使用xml文件,用注解实现
public List<User> getUser(); //2、使用xml文件实现,
//方法名需要同UserMapper.xml的相同。
}
service具体实现层
UserServiceImpl.java
package com.springboot.chapter4.service.impl;
import com.springboot.chapter4.mapper.UserMapper;
import com.springboot.chapter4.entity.User;
import com.springboot.chapter4.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created on 2019/8/15 by Tinchi
**/
@Service //标识为service
@Transactional //开启事务注解
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
//1、不使用xml文件,用注解实现
@Override
public List<User> getAllUser() {
return userMapper.getAllUser();
}
//1、使用xml文件实现,方法名需要同UserMapper.xml的相同
@Override
public List<User> getUser() {
return userMapper.getUser();
}
}
mapper层
UserMapper.java
package com.springboot.chapter4.mapper;
import com.springboot.chapter4.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created on 2019/8/15 by Tinchi
**/
@Repository //标识为持久层
@Mapper //标识为mapper文件
public interface UserMapper {
//1:不需要xml文件的实现方式,需要写@Mapper注解,同时使用@Select写明SQL语句
//或者在启动springboot入口文件,
//配置Mapper的位置(例如@MapperScan("com.springboot.chapter4.mapper"))。
@Select("select * from user")
public List<User> getAllUser();
//2:使用xml文件实现sql语句的查询
public List<User> getUser();
}
mapper映射文件
UserMapper.xml
<?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.springboot.chapter4.mapper.UserMapper">
<resultMap id="user" type="com.springboot.chapter4.entity.User">
<result property="id" column="id"/>
<result property="userName" column="userName"/>
<result property="note" column="note"/>
</resultMap>
<!--mapper层的方法名需要同该xml文件的select的id名一致-->
<select id="getUser" resultMap="user">
select * from user
</select>
</mapper>
属性配置文件
application.properties
#数据库配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&serverTimezone=UTC&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=sise
#mybatis配置,有标识别名的实体层
mybatis.type-aliases-package=com.springboot.chapter4.entity
#mybatis映射文件通配
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
#mybatis配置文件
#mybatis.config-location=classpath:mybatis/mybatis-config.xml
#日志配置
logging.level.sql=debug
logging.level.org.springframework=debug
logging.level.org.org.mybatis=debug
启动类
SpringbootAopApplication.java
package com.springboot.chapter4;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//@MapperScan("com.springboot.chapter4.mapper")
public class SpringbootAopApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAopApplication.class, args);
}
}
MySQL
记得打开数据库服务,更改配置文件的账号密码。然后启动,在浏览器输入:http://localhost:8080/user/getAllUser 或者 http://localhost:8080/user/getUser
即可看到效果,本文主要介绍获取所有user的两种实现方式。