本文为Spring Boot2.1系列的第五篇,代码可以从github下载 https://github.com/yinww/demo-springboot2.git
本章介绍Springboot 使用Mybatis。
一、准备数据库
create database demo005;
create table user
(
id bigint not null,
name varchar(64),
primary key (id)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8;
二、创建工程demo005
pom.xml的内容为
4.0.0
com.yinww
demo-springboot2
0.0.1-SNAPSHOT
demo005
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
三、Java代码
主类
package com.yinww.demo.springboot2.demo005;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo005Application {
public static void main(String[] args) {
SpringApplication.run(Demo005Application.class, args);
}
}
User类
package com.yinww.demo.springboot2.demo005.domain;
public class User {
private Long id;
private String name;
// getter and setter
}
UserMapper
package com.yinww.demo.springboot2.demo005.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.yinww.demo.springboot2.demo005.domain.User;
@Mapper
public interface UserMapper {
void addUser(User user);
}
UserService
package com.yinww.demo.springboot2.demo005.service;
import com.yinww.demo.springboot2.demo005.domain.User;
public interface UserService {
void addUser(User user);
}
UserServiceImpl
package com.yinww.demo.springboot2.demo005.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yinww.demo.springboot2.demo005.domain.User;
import com.yinww.demo.springboot2.demo005.mapper.UserMapper;
import com.yinww.demo.springboot2.demo005.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
}
Controller, 提供了一个存储数据的接口
package com.yinww.demo.springboot2.demo005.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yinww.demo.springboot2.demo005.domain.User;
import com.yinww.demo.springboot2.demo005.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("add")
public Object addUser() {
User user = new User();
user.setId(new Long(1));
user.setName("张三丰");
userService.addUser(user);
return "ok";
}
}
四、配置文件
UserMapper.xml
insert into user (id, name) values(#{id}, #{name})
application.properties 主要配置redis
spring.application.name=demo005
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo005?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
mybatis.typeAliasesPackage=com.yinww.demo.springboot2.demo005.domain
mybatis.mapperLocations=classpath:mapper/*.xml
五、运行
执行
java -jar demo005-0.0.1-SNAPSHOT.jar
也可以以其他方式启动。
添加数据
查看数据库,可以看到数据已经保存成功
mysql> select * from user;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 张三丰 |
+----+-----------+
1 row in set (0.00 sec)
本文内容到此结束,更多内容可关注公众号: