1.首先上依赖
<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>
<!--lombok插件依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--springboot整合mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2.配置文件application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_mybatis?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
###在springboot2.0之前的版本 有些变量的名称不一样
###spring.datasource.driverClassName=com.mysql.Driver 2.0之前是这样配置的 2.0之后如果这样 会报错 找不到driverclassname
###因为我的8080端口被占用了,所以换一个端口号
server.port=8888
配置的时候,注意一下自己的springboot版本。2.0以前和2.0以后的 在某些变量的名称上会有改变,如果配置不恰当,会报错。
在2.0之前,是spring.datasource.driverClassName=com.mysql.Driver
在2.0之后,是spring.datasource.driver-class-name
一定要注意。
?serverTimezone=UTC
在数据库名称后面添加这个,是因为如果你mysql版本太高,会报错:
You must configure either the server or JDBC driver (via the serverTimezone cconfig
UTC是统一世界标准时间,加上?serverTimezone=UTC这个就解决了。
具体可以参照我另一篇博客:
(使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone cconfig - weixin_42652696的博客 - CSDN博客 https://blog.csdn.net/weixin_42652696/article/details/83864740)
3.编写entity、mapper、service、controller等
数据库的表如下图:
项目的结构如下:
接下来上代码
User类:
package com.example.demo.entity;
import lombok.Data;
/**
* Created by Admin on 2018-11-08.
*/
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
UserMapper接口:
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
* Created by Admin on 2018-11-08.
*/
@Repository
//@Mapper
public interface UserMapper {
//新增方法+sql语句
@Insert("INSERT INTO USERS(NAME,AGE)VALUES(#{name},#{age})")
int insertMsg(@Param("name")String name,@Param("age")Integer age);
}
@Mapper注解之所以被我注释掉,
是因为我在启动类里面加了@MapperScan(basePackages = "com.example.demo.mapper")注解
这样,就不需要每个mapper接口都添加@Mapper注解了,通过@MapperScan扫描包的方式更省事一点。
UserService接口:
package com.example.demo.service;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Created by Admin on 2018-11-08.
*/
@org.springframework.stereotype.Service
public interface UserService {
int insertMsg(String name,Integer age);
}
UserServiceImpl类:
package com.example.demo.service.impl;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Admin on 2018-11-08.
*/
@Service
@Log
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public int insertMsg(String name, Integer age) {
int insertMsgResult = userMapper.insertMsg(name,age);
log.info("日志输出为:"+insertMsgResult);
return insertMsgResult;
}
}
UserController类:
package com.example.demo.controller;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Admin on 2018-11-08.
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("insertMsg")
public String insertMsg(String name,Integer age){
userService.insertMsg(name,age);
return "成功添加数据";
}
}
启动类:
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
到此,就ok了。运行程序,在地址栏中中访问localhost:8888/insertMsg?name=xxx&age=xx
如图:
全demo到此结束了,如果有什么问题建议,欢迎在下方留言,我每天都会看的。谢谢大家