一,在mysql数据库中创建测试表User
use test;
CREATE TABLE USER (
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(20) NOT NULL,
age int
);
select * from user;
二,创建普通maven项目
三,pom.xml中导入相关依赖包
<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>com.springboot</groupId>
<artifactId>springboot-mybatis01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- spring-boot-starter-parent 整合常用第三方框架依赖信息 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
</dependency>
<!-- springboot - web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
</dependency>
<!-- lombok插件 (注意:eclipse需要集成lombok插件,可自行百度) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
四,sourses文件夹下创建配置文件 application.properties 设置连接mysql的参数,内容如下
###datasource
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource..password=a
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
五,创建类
实体层: User
package com.springboot.entity;
/**
* 实体类
*/
import lombok.Data;
@Data // 集成了lombok之后,可自动生成get,set方法
public class User {
private Integer id;
private String name;
private Integer age;
}
sql语句映射类:UserMapper
package com.springboot.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.springboot.entity.User;
public interface UserMapper {
// 查询
@Select("select * from user where name=#{name}")
User findByName(@Param("name") String name);
// 添加
@Insert("insert into user(name,age) values(#{name},#{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
}
业务层:UserService
package com.springboot.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.entity.User;
import com.springboot.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j // 等同于private Logger logger = LoggerFactory.getLogger(this.getClass());
public class UserService {
@Autowired
private UserMapper userMapper;
public User findByName(String name) {
User user = userMapper.findByName(name);
log.info("查询结果:" + user.toString());
return user;
}
public int insert(String name, Integer age) {
int result = userMapper.insert(name, age);
log.info("插入结果:" + result);
return result;
}
}
控制层:UserController
package com.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.entity.User;
import com.springboot.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("findByName")
public User findByName(String name) {
return userService.findByName(name);
}
@RequestMapping("insert")
public int insert(String name, Integer age) {
return userService.insert(name, age);
}
}
启动类:App
package com.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 将启动的代码抽取出来,单独运行
*
* @author vean
*
*/
@SpringBootApplication // 等同于@EnableAutoConfiguration+@ComponentScan(只扫描同级包<包含子包>和当前包)
@MapperScan(basePackages = { "com.springboot.mapper" }) // 扫描映射文件。 注意:mybatis启东时候,在mapper层可以不加mapper注解,但是一定要在启动类上加@mapperScan注解
public class App {
public static void main(String[] args) {
// 整个程序的入口,启动springboot项目,创建内置tomcat服务器,使用tomcat加载springmvc 注解启动类
SpringApplication.run(App.class, args);
}
}
六,演示
添加一条用户信息:
日志信息:
查找用户信息:
日志信息:
数据库内容: