问题分析:
1---数据库连接问题
2---@Param注解添加问题
3---Parameter 'username' not found. Available parameters are [user, param1]报错:
单独 将user对象中的参数分别拆开进行封装
操作数据库采用的是mapper.xml文件(也可以采用注解方式,前面文章有讲述)
注意:1)dao层中的接口类的方法名称要和mapper中的id数字那个保持一致才不会出错,
2)同时接口类的名称,例如UserController接口和UserMapper.xml名称要保持一致
3)UserController类和UserMapper.xml
(1)可以同时放在dao包下面,此时需要单独配置Usermapper.xml的映射信息
(2)可以将UserMapper.xml文件直接放在Resources中的mapper文件夹中,此时建立的
是directory目录,只需要在application.yml配置文件中指定相关的路径信息即可;
4)
1---工程结构如下
2--工程过程
1--IDEA新建SpringBoot工程
2--创建数据库,创建数据表
3--SpringBoot中pom文件设置,安装MySql,MyBatis,lombok,Thymeleaf,Web相关依赖等
4--创建实体类User,添加注解@Data,再添加有参和无构造方法,或者重写toString方法
4--创建UserMapper接口,在类名前加上@Mapper注解,并根据需求加上@param限制
5--创建UserService服务层,添加@Service服务层,
=======使用@Autowired注解注入UserMapper到服务层
6--创建UserController控制层,类前加上@Controller和@RuestMapping注解,
=======使用@Autowired注入UserService到controlle,
在这一层编写相关的增删改查的方法和映射地址
7--配置application.yml中的数据库连接信息,端口号信息,mybatis中mapper映射文件信,日志信息等
8--在resources中新建目录mapper,新建UsermMapper.xml文件
9--在src/main/resources目录下新建static/css文件夹,用于存放静态资源,并将bootstrap.css文件
放入其中。
10--在src/main/resources目录下新建templates/user文件夹,用于存放html页面。
11--在templates/user目录中新建list.html文件:
12--在templates/user目录中新建addPage.html文件:
13--在templates/user目录中新建updatePage.html文件:
14--编写启动类:
15--启动项目,到浏览器进行运行测试
16--运行测试结果截图如下:
2--创建数据库和数据表:
3--IDEA中pom文件依赖:
<?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 https://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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</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.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4--创建实体类User:
package com.example.demo.bean;
import lombok.Data;
/**
* Created by NanTian
* on 2019/11/15 9:26
*/
@Data
public class User {
private Integer id;
private String username;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
}
4---创建UserMapper接口:
package com.example.demo.dao;
import com.example.demo.bean.User;
import jdk.internal.dynalink.linker.LinkerServices;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Created by NanTian
* on 2019/11/15 9:30
*/
@Mapper
public interface UserMapper {
/*
* mybatis会自动识别user对象的值,传到xml文件中,所以需要给对象指定映射。
* 在userDao方法中给user对象添加上@Param注解,可解决这个问题。
* */
int add(@Param("username")String username,@Param("password") String password);
int delete(@Param("id") Integer id);
int update(@Param("id")Integer id,@Param("username")String username,@Param("password")String password);
List<User> list();
User findById(@Param("id") Integer id);
}
5---创建UserService服务层:
package com.example.demo.service;
import com.example.demo.bean.User;
import com.example.demo.dao.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by NanTian
* on 2019/11/15 9:32
*/
@Service
public class UserService implements UserMapper{//实现接口 实现接口的所有方法
@Autowired(required = false)
private UserMapper userMapper;
@Override
public int add(@Param("username")String username,@Param("password") String password) {
return userMapper.add(username,password);
}
@Override
public int delete(Integer id) {
return userMapper.delete(id);
}
@Override
public int update(@Param("id")Integer id,@Param("username")String username,@Param("password")String password) {
return userMapper.update(id,username,password);
}
@Override
public List<User> list() {
return userMapper.list();
}
@Override
public User findById(Integer id) {
return userMapper.findById(id);
}
}
6---创建UserController控制层:
package com.example.demo.controller;
import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/*
* 1--创建数据库,创建数据表
3--SpringBoot中pom文件设置,安装MySql,MyBatis,lombok,Thymeleaf,Web相关依赖等
4--创建实体类User,添加注解@Data,再添加有参和无构造方法,或者重写toString方法
4--创建UserMapper接口,在类名前加上@Mapper注解,并根据需求加上@param限制
5--创建UserService服务层,添加@Service服务层,=======使用@Autowired注解注入UserMapper到服务层
6--创建UserController控制层,类前加上@Controller和@RuestMapping注解,=======使用@Autowired注入UserService到controller
在这一层编写相关的增删改查的方法和映射地址
7--配置application.yml中的数据库连接信息,端口号信息,mybatis中mapper映射文件信,日志信息等
8--在resources中新建目录mapper,新建UsermMapper.xml文件
9--在src/main/resources目录下新建static/css文件夹,用于存放静态资源,并将bootstrap.css文件放入其中。
10--在src/main/resources目录下新建templates/user文件夹,用于存放html页面。
11--在templates/user目录中新建list.html文件:
12--在templates/user目录中新建addPage.html文件:
13-- 在templates/user目录中新建updatePage.html文件:
14--编写启动类:
15--启动项目,到浏览器进行运行测试
16--运行测试结果截图如下:
2--
* */
/**
* Created by NanTian
* on 2019/11/15 9:35
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 查询所有的方法
* @param model
* @return
*/
@RequestMapping("/list")
public String list(Model model){
List<User> list = userService.list();
model.addAttribute("list",list);
return "user/list";
}
/**
* 用户添加页面
* @return
*/
@RequestMapping("/addPage")
public String addPage(){
return "user/addPage";
}
/**
* 添加用户
* @param username
* @param password
* @return
*/
@RequestMapping("/add")
public String add(@Param("username")String username,@Param("password")String password){
userService.add(username,password);
return "redirect:/user/list";
}
/**
* 用户修改页面
* @param model
* @param id
* @return
*/
@RequestMapping("/updatePage")
public String updatePage(Model model,Integer id){
User user = userService.findById(id);
model.addAttribute("user",user);
return "user/updatePage";
}
/**
* 修改用户
* @param username
* @param password
* @return
*/
@RequestMapping("/update")
public String update(@Param("id")Integer id, @Param("username")String username,@Param("password")String password){
userService.update(id,username,password);
return "redirect:/user/list";
}
/**
* 删除用户
* @param id
* @return
*/
@RequestMapping("delete")
public String delete(Integer id){
userService.delete(id);
return "redirect:/user/list";
}
}
7---配置application.yml文件信息:
server:
port:8080
context-path:/
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
thymeleaf:
cache: false
mybatis:
mapper-locations: classpath:mapper/*.xml
logging:
level:
com.example.demo.dao: DEBUG
8---创建UserMapper.xm文件:
<?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="com.example.demo.dao.UserMapper"><!--数据库接口的包-->
<!--sql语句中对应的id和mapper接口中的方法的名字保持一致性-->
<insert id="add">
insert into t_user values(null,#{username},#{password})
</insert>
<delete id="delete">
delete from t_user where dbid=#{id}
</delete>
<update id="update" >
update t_user set dbid=#{id},user_name=#{username},password=#{password} where dbid=#{id}
</update>
<select id="list" resultMap="userMap">
select * from t_user
</select>
<select id="findById" resultMap="userMap">
select * from t_user where dbid=#{id}
</select>
<!--结果集映射 property是bean中的字段-->
<resultMap id="userMap" type="com.example.demo.bean.User">
<id property="id" column="dbid"/>
<result property="username" column="user_name"/>
<result property="password" column="password"/>
</resultMap>
</mapper>
9---下载bootstrap.css文件并存放:可去bootstrap官网下载:
10---建立user文件夹:
11---利用Thymeleaf新建list.html文件:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户管理</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
</head>
<body class="container">
<br>
<h1>yonghuliebiao</h1>
<br>
<br>
<div class="with:80%">
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>UserName</th>
<th>Password</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="user:${list}">
<th scope="row" th:text="${user.id}">1</th>
<td th:text="${user.username}">neo</td>
<td th:text="${user.password}">nOtto</td>
<td><a th:href="@{/user/updatePage(id=${user.id})}">edit</a></td>
<td><a th:href="@{/user/delete(id=${user.id})}">delete</a></td>
</tr>
</tbody>
</table>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">
<a th:href="@{/user/addPage}" class="btn btn-info">add</a>
</div>
</div>
</body>
</html>
12---新建addPage.html文件:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户管理</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
</head>
<body class="container">
<br>
<h1>add customer</h1>
<br>
<br>
<div class="with:80%">
<form class="form-horizontal" th:action="@{/user/add}" method="post">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username" id="username" placeholder="username">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" id="password" placeholder="passsword">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="Submit" class="btn btn-info"/>
<input type="reset" value="Reset" class="btn btn-info"/>
</div>
</div>
</form>
</div>
</body>
</html>
13---新建updatePage.html文件:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户管理</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
</head>
<body class="container">
<br>
<h1>edit customer</h1>
<br>
<br>
<div class="with:80%">
<form class="form-horizontal" th:action="@{/user/update}" th:object="${user}" method="post">
<input type="text" name="id" th:value="*{id}">
<div class="form-group">
<label for="userName" class="col-sm-2 control-label">username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username" id="username" th:value="*{username}" placeholder="username">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" id="password" th:value="*{password}" placeholder="passsword">
</div>
</div>
<!--返回主页面信息-->
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="Submit" class="btn btn-info"/>
<a th:href="@{/user/list}" class="btn btn-info">Back</a>
</div>
</div>
</form>
</div>
</body>
</html>
14--启动类改写:添加扫描包
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo.controller","com.example.demo.service"})
@MapperScan(basePackages = "com.example.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
15:测试总览页:
16---测试结果:
编辑更新并提交:
新增页面并提交: