文章目錄
一、項目介紹和下載
我們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