1、新建一个springboot项目
(1)选择spring initializr
(2)选择jdbc,Mysql,MyBatis
2、配置pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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.1.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>
<!--导入druid连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.4</version>
</dependency>
<!--使用这个druit的依赖,则不需要指定版本号,上面的引入依赖的方式就是需要写版本号的
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>-->
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
需要导入druid连接池依赖,并且有两种情况,具体请看上面代码显示的内容。
3、配置application.yml文件
spring:
datasource:
# 数据源基本配置
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 19970710
# 数据源其他配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
# 指定全局配置文件位置
config-location: [color=#FF0000]classpath:mybatis/mybatis-config.xml[/color]
# 指定sql映射文件位置
mapperLocations: [color=#FF0000]classpath:mybatis/mapper/*.xml[/color]
typeAliasesPackage:[color=#FF0000] com.example.entity[/color]
mybatis全局配置文件和sql映射文件位置:
[img=https://img-bbs.csdn.net/upload/201910/13/1570952095_314305.jpg][/img]
(1)mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
<!--mybatis的总配置文件mybatis-config.xml-->
(2)UserMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="[color=#FF0000]com.example.dao.UserDao[/color]">
<select id="[color=#FF0000]selectUser[/color]" resultType="[color=#FF0000]com.example.entity.User[/color]">
select * from user where username = #{username} and password = #{password};
</select>
<!--新增-->
<insert id="[color=#FF0000]insertUser[/color]">
insert user (username,password) values (#{username},#{password});
</insert>
</mapper>
说明:namespace="com.example.dao.UserDao",当namespace绑定的是dao接口的全路径时,其实现数据持久化的方式为有代理模式实现数据持久化。即会自动产生代理,自动实现数据的持久化,不需要实现dao层的接口。selectUser,insertUser就是该接口中的方法。
4、实体类User
package com.example.entity;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
5、dao层UserDao接口
package com.example.dao;
import com.example.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao {
public void insertUser(@Param("username")String username, @Param("password")String password);
public User selectUser(@Param("username")String username,@Param("password")String password);
}
PS:比较需要注意的一点是,当遇到传入多个参数的问题时,就需要在Dao中每个参数前面加上“@Param”注解,括号中注解的名称就是mapper中映射的值。
6、服务层
package com.example.service;
import com.example.dao.UserDao;
import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl {
@Autowired
public UserDao userDao;
public boolean insertUser(String username,String password){
userDao.insertUser(username,password);
return true;
}
public User selectUser(String username,String password){
System.out.println(username+" " +username);
return userDao.selectUser(username,password);
}
}
7、控制层controller
package com.example.controller;
import com.example.entity.User;
import com.example.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
public class UserController {
@Autowired
private UserServiceImpl userServiceImp;
@RequestMapping("/login")
public String login(){
return "login";
}
@RequestMapping("/register")
public String register(){
return "register";
}
@RequestMapping("/dologin")
public String doLogin(User user, Map<String,Object> map){
User user1 = userServiceImp.selectUser(user.getUsername(),user.getPassword());
System.out.println(user1);
if(user1==null){
map.put("msg","密码或账户错误!");
return "fail";
}else{
map.put("mag","登入成功");
return "success";
}
}
@RequestMapping("/doregister")
public String doRegister(User user,Map<String,Object> map){
userServiceImp.insertUser(user.getUsername(),user.getPassword());
map.put("msg","注册成功");
return "success";
}
}
PS:/dologin和/doregister都是要和前端进行连接操作的
8、login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录界面</title>
<style type="text/css">
ul li{
list-style: none;
}
</style>
</head>
<body>
<form name="form" accept-charset="utf-8" action="/dologin">
<ul>
<li>
<label class="input-tips2">用户名:</label>
<div class="inputOuter2">
<input type="text" id="username" name="username" maxlength="16" />
</div>
</li>
<li>
<label class="input-tips2">密码:</label>
<div >
<input type="password" id="password" name="password" maxlength="16"/>
</div>
</li>
<li>
<div >
<input type="submit" value="登录"/>
<a href="/register">注册</a>
</div>
</li>
</ul>
</form>
</body>
</html>
9、register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册界面</title>
</head>
<body>
<form name="form" accept-charset="utf-8" action="/doregister">
<ul >
<li>
<label class="input-tips2">用户名:</label>
<div class="inputOuter2">
<input type="text" id="username" name="username" maxlength="16" />
</div>
</li>
<li>
<label class="input-tips2">密码:</label>
<div >
<input type="password" id="password" name="password" maxlength="16"/>
</div>
</li>
<li>
<div>
<input type="submit" value="确定"/>
</div>
</li>
</ul>
</form>
</body>
</html>
10、success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<p>注册或者登陆成功 success</p>
</body>
</html>
11、最后我一直遇到这个问题:
Field userDao in com.example.service.UserServiceImpl required a bean of type 'com.example.dao.UserDao' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
原因就是在mapper文件夹下面的xxmapper.xml文件没有扫到com.example.dao这个包,所以要在Application(启动类)上加上注解:
@MapperScan("com.example.dao")就可以解决了。
12、附上项目目录: