spring.datasource.url=jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
创建SpringBoot工程加入SpringMVC和Mybatis(SQL Driver,MyBatis Frame)
CREATE TABLE product(id INT PRIMARY KEY AUTO_INCREMENT,title VARCHAR(50),
price DOUBLE(10,2),num INT);
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>商品管理系统h1>
<a href="/insert.html">添加商品a>
<a href="/select">商品列表a>
<a href="/update.html">商品修改a>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/insert">
<input type="text" name="title" placeholder="商品标题">
<input type="text" name="price" placeholder="商品价格">
<input type="text" name="num" placeholder="商品库存">
<input type="submit" value="添加商品">
form>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>修改页面h1>
<form action="/update">
<input type="text" name="id" placeholder="请输入要修改的商品编号">
<input type="text" name="title" placeholder="请输入商品名称">
<input type="text" name="price" placeholder="请输入商品价格">
<input type="text" name="num" placeholder="请输入库存数量">
<input type="submit" value="提交">
form>
body>
html>
package cn.tedu.boot03.entity;
public class Product {
private Integer id;
private String title;
private Double price;
private Integer num;
@Override
public String toString() {
return "Product{" +
"id=" + id +
", title='" + title + '\'' +
", price=" + price +
", num=" + num +
'}';
}
public Product() {
}
public Product(Integer id, String title, Double price, Integer num) {
this.id = id;
this.title = title;
this.price = price;
this.num = num;
}
//set,get省略
package cn.tedu.boot03.mapper;
import cn.tedu.boot03.entity.Product;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface ProductMapper {
//#{变量名} 会自动找到下面方法中参数列表里面的同名参数,
// 如果没有同名参数的话会调用参数列表中对象的getXXX方法
@Insert("insert into product values(null,#{title},#{price},#{num})")
void insert(Product product);
//Select注解执行查询相关的SQL语句,查询到的数据会自动封装到Product对象中并且把
//多个对象添加到list集合中
@Select("select * from product")
List<Product> select();
@Delete("delete from product where id=#{id}")
int deleteById(int id);
@Update("update product set title=#{title},price=#{price},num=#{num} where id=#{id}")
int update(Product product);
}
package cn.tedu.boot03.controller;
import cn.tedu.boot03.entity.Product;
import cn.tedu.boot03.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ProductController {
//自动装配注解,此注解是Spring框架中提供的注解,添加此注解后Spring框架和MyBatis框架
//会创建一个实现类(实现了ProductMapper接口),并且实例化了此实现类,把实例化后的对象赋值给了mapper变量
@Autowired(required = false)//如果Autowired注解报错则添加(required=false) 解决误提示
ProductMapper mapper;
@RequestMapping("/insert")
public String insert(Product product){
System.out.println("product = " + product);
mapper.insert(product);
return "添加完成!返回首页";
}
@RequestMapping("/select")
public String select(){
List<Product> list = mapper.select();
String html="";
html+="商品列表 ";
html+="id 标题 价格 库存 操作 ";
for(Product product:list){
html+="";
html+=""+ product.getId()+" ";
html+=""+ product.getTitle()+" ";
html+=""+ product.getPrice()+" ";
html+=""+ product.getNum()+" ";
html+="删除 ";
html+=" ";
}
html+="
";
return html;
}
@RequestMapping("/delete")
public String delete(int id){
int count = mapper.deleteById(id);
System.out.println("id = " + id);
if(count>0){
return "删除成功返回列表页面";
}
return "删除失败返回列表页面";
}
@RequestMapping("/update")
public String update(Product product){
int count=mapper.update(product);
if (count>0){
return "修改成功返回主页面";
}
return "修改失败返回修改页面";
}
}
自动装配注解,此注解是Spring框架中提供的注解,添加此注解后Spring框架和MyBatis框架
会创建一个实现类(实现了ProductMapper接口),并且实例化了此实现类,把实例化后的对象赋值给了mapper变量
@Autowired(required = false)//如果Autowired注解报错则添加(required=false) 解决误提示
ProductMapper mapper;
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(50),PASSWORD VARCHAR(50),nick VARCHAR(50),
)CHARSET=utf8;
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>工程首页h1>
<a href="/reg.html">注册a>
<a href="/login.html">登录a>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>登录页面h1>
<form action="/login">
<input type="text" name="username" placeholder="请输入用户名">
<input type="text" name="password" placeholder="请输入密码">
<input type="submit" 登录>
form>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>注册页面h1>
<form action="/reg">
<input type="text" name="username" placeholder="用户名">
<input type="text" name="password" placeholder="密码">
<input type="text" name="nick" placeholder="昵称">
<input type="submit" value="注册">
form>
body>
html>
public class User {
private Integer id;
private String username;
private String password;
private String nick;
public User() {
}
public User(Integer id, String username, String password, String nick) {
this.id = id;
this.username = username;
this.password = password;
this.nick = nick;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nick=" + nick +
'}';
//省略get/set方法
}
@Mapper
public interface UserMapper {
@Select("select id,username,password,nick from user where username=#{username}")
User selectByName(String username);
@Insert("insert into user values(null,#{username},#{password},#{nick})")
int insert(User user);
}
@RestController
public class UserController {
@Autowired(required = false)
UserMapper mapper;
@RequestMapping("/login")
public String login(User user){
System.out.println("user = " + user);
User u = mapper.selectByName(user.getUsername());
if(u!=null){
if (u.getPassword().equals(user.getPassword())){
return "登录成功";
}
return "密码错误";
}
return "用户名不存在";
}
@RequestMapping("/reg")
public String reg(User user){
System.out.println("user = " + user);
//查询注册用户是否存在
User u = mapper.selectByName(user.getUsername());
if(u!=null){
return "用户名存在";
}
int count=mapper.insert(user);
if (count>0){
return "注册成功";
}
return "注册失败";
}
}