文章目录
一、项目介绍和下载
我们springboot2.0+mybatis+mysql搭建javaweb项目,全部都使用注解,实现数据库的插入和查询。
项目下载: https://github.com/enjoyu/mybatis
二、项目结构
1、application.properties配置文件中填写数据库信息。
2、工程中包括实体类、接口层、服务层、前端控制层。
3
三、步骤
1.创建用户表
create database test;
use test;
create table user(
id int primary key auto_increment,
name varchar(20),
age int
);
2.新建项目
1、新建项目create new project
2、Spring Initializr,点击next
3、group填写com.springboot,Artifact填写mybatis,点击next
4、添加依赖,选择web、mysql、mybatis,点击next
5、点击finish,项目创建成功
6、项目目录结构如下:
7、查看pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.修改配置文件application.properties
#mysql数据库配置(数据库驱动、数据库地址、用户名、密码)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root123
4.代码
4.1实体类user.java
在com.springboot.mybatis包下创建子包entity,在entity下创建user.java。
package com.springboot.mybatis.entity;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4.2接口层userMapper.java
在com.springboot.mybatis包下创建子包mapper,在mapper下创建userMapper.java。
package com.springboot.mybatis.mapper;
import com.springboot.mybatis.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper //声明UserMapper为一个Mapper接口
public interface UserMapper {
/*@Result是结果映射列表
property是User类的属性名,colomn是数据库表的字段名
*/
@Results({
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "age", column = "age")
})
//插入用户
@Insert("INSERT INTO user(name, age) VALUES (#{name}, #{age})")
void insert(User user);
//根据年龄查询用户
@Select("SELECT * FROM user WHERE age = #{age}")
List<User> select(int age);
}
4.3服务类userService.java
在com.springboot.mybatis包下创建子包service,在service下创建userService.java。
package com.springboot.mybatis.service;
import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service //声明为服务
public class UserService {
@Autowired //注入userMapper
private UserMapper userMapper;
public String show() {
return "Hello World!";
}
//插入用户
public String insert(String name, int age) { //插入一条记录
User user = new User();
user.setName(name);
user.setAge(age);
userMapper.insert(user);
return "Insert ( \""+name+"\", age"+age+") OK!";
}
//根据年龄查询用户
public List<User> select(int age) {
return userMapper.select(age);
}
}
4.4前端控制类userController.java
在com.springboot.mybatis包下创建子包controller,在controller下创建userController.java。
package com.springboot.mybatis.controller;
import com.springboot.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //声明为一个Restful的Controller
public class UserController {
@Autowired //注入userService
private UserService userService;
//显示
@RequestMapping(value = "/show")
public String show() {
return userService.show();
}
//插入用户
@RequestMapping(value="/insert")
public String insert(String name, int age) {
return userService.insert(name, age);
}
//根据年龄查询用户
@RequestMapping(value = "/select")
public Object select(int age) {
return userService.select(age);
}
}
4.5启动类application.java
默认就行
package com.springboot.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
四、运行结果
运行MybatisApplication类启动成功如下图所示:
1.显示helloworld
访问 http://localhost:8080/show ,结果如下
2.插入用户
访问 http://localhost:8080/insert?name=小明&age=20 ,结果如下:
用户表中插入了相关记录:
3.根据年龄查询用户
访问 http://localhost:8080/select?age=20 ,结果如下
4.缺少参数,查询用户,出错
访问 http://localhost:8080/select ,结果如下:
这个是一个白页,是说缺少参数age
五、遇到的问题
问题1: Could not autowire. No beans of ‘UserMapper’ type found.
解决方法: https://blog.csdn.net/hju22/article/details/88136926
问题2: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized
解决方法: https://blog.csdn.net/hju22/article/details/88192989