pom.xml引入代码
<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>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>
</dependency>
<!-- springboot-web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.yml引入
###springboot2.0名称url在多数据源的情况下变成jdbc-url,否则出现debug
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123123
driver-class-name: com.mysql.jdbc.Driver
mapper代码注解注入
package com.xyt.springboot.mapper;
import com.xyt.springboot.entity.Users;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select("SELECT * FROM USERS WHERE NAME = #{name}")
Users findByName(@Param("name") String name);
@Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
}
controller控制器代码
package com.xyt.springboot.controller;
import com.xyt.springboot.entity.Users;
import com.xyt.springboot.mapper.UserMapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.xml.transform.Result;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/addUser")
public String addUser(String name, int age) {
return userMapper.insert(name, age) > 0 ? "success" : "fail";
}
}
Users实体类代码
package com.xyt.springboot.entity;
public class Users {
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@SpringBootApplication启动项目
package com.xyt.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.xyt.springboot.mapper")// 在 启动类中 添加 @MapperScan 注解 扫描 mapper
public class AppSpringBootMybatis {
public static void main(String[] args) {
SpringApplication.run(AppSpringBootMybatis.class, args);
}
}
代码结构
数据库代码
/*
Navicat MySQL Data Transfer
Source Server : 我的本地测试库
Source Server Version : 50540
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50540
File Encoding : 65001
Date: 2019-11-15 14:25:46
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;