前提
jdk idea maven都安装好了!第一个demo你也跟我敲了,我是2020-04-22日下午19:00入门的;
看这个例子https://blog.csdn.net/tian_jiangnan/article/details/105688801
我们先实现html页面与后台的数据交互,然后再实现后台与数据库部分的交互
我接着上一个例子讲 https://blog.csdn.net/tian_jiangnan/article/details/105688801
上一个例子只是了解springboot的配置与运行;
html页面与后端数据交互
在templates文件夹上面新建一个h5文件,我的名字是index
然后写一个简单的表单
我想一个类里面有很多方法,每个方法映射的路径不一样,如果项目很多,就会混,不知道哪对哪?
我们需要一个二级路径/user/reg 我们给这个类映射一个路径为user,方法映射一个路径为reg,这样我们就知道哪对哪了
我写了一个路径/user/reg
我们想要直接访问Html页面的话,需要进行配置
pom.xml添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.properties
# 定位模板的目录
spring.thymeleaf.prefix=classpath:/templates/
新建一个IndexControl类
package com.example.demo.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexControl {
@RequestMapping("/index")
public String index(){
return "index";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/user/reg">
<input name="username" type="text" placeholder="请输入用户名"/>
<input name="password" type="password" />
<input type="submit" value="注册">
</form>
</body>
</html>
新建一个控制器类
package com.example.demo.control;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class DemoControl {
@RequestMapping("/reg")
public String hello(String username,String password){
System.out.println(username);
System.out.println(password);
return "欢迎进入springboot的世界"+username+"密码是"+password;
}
}
现在我们运行项目测试看看
输入用户名与密码点击注册
是不是显示了,前后端已经交互了!
后端与数据库交互
1、在pom.xml里面导入mybatis以及Mysql依赖包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>runtime</scope>
</dependency>
又是红色的,我们右击项目,maven->Reimport
自动更新依赖包
删除原来的 application.properties ,新建 application.yml,因为 application.yml更整洁
我们把之前的配置加进来
spring:
thymeleaf:
prefix: classpath:/templates/
添加数据库连接信息
数据库名为app
com.mysql.cj.jdbc.Driver
spring:
thymeleaf:
prefix: classpath:/templates/
datasource:
url: jdbc:mysql://localhost:3306/app?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
我们现在在resources文件夹下面新建一个文件夹mapper,这里放表与类的映射文件
application.yml 增加MyBatis配置信息
mybatis:
mapper-locations: classpath:mapper/*.xml #对应mapper映射xml文件所在路径
type-aliases-package: cn.wbnull.springbootdemo.model #对应实体类路径
现在新建一个包Model,里面存放实体类
我们新建一个类User
package com.example.demo.model;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setId(int id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
现在我们要实现后台与数据库的交互
dao是接口,service是实现类
然后我们再新建一个包dao
再次基础上新建一个UserDao接口
这里是实现了两个方法,一个是查询一个是插入,当然还是接口
package com.example.demo.dao;
import com.example.demo.model.User;
import java.util.List;
public interface UserDao {
//查询功能
List<User> query();
// 插入功能
int insert(User user);
}
我们再来mapper文件夹里面配置User类的信息
新建一个UserMapper.xml文件
<select> 表示这是一条查询语句, id=“query” 一定要与dao层需要匹配的方法名一致,resultMap 表示对应的返回值类型
<?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.UserDao">
<resultMap id="BaseResultMap" type="com.example.demo.model.User">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id, username,password
</sql>
<select id="query" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user
</select>
<insert id="insert" parameterType="com.example.demo.model.User">
insert into user (id, username,password
)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
</insert>
</mapper>
我们再新建一个Service层
新建一个UserService类
package com.example.demo.service;
import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserDao userdao;
public List<User> query(){
return userdao.query();
}
public int insert(String username,String password){
User u=new User();
u.setUsername(username);
u.setPassword(password);
return userdao.insert(u);
}
}
这个时候我们需要新建一个类,这个类要实现前端与后台的交互,这一层叫control层
package com.example.demo.control;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Scope("prototype")
@RequestMapping("/user")
public class UserControl {
@Autowired
private UserService us;
@RequestMapping("/query")
public List<User> query() throws Exception{
return us.query();
}
@RequestMapping("/insert")
public int insert(@RequestParam(value = "username") String username,@RequestParam(value = "password") String password) throws Exception{
System.out.println(username);
System.out.println(password);
return us.insert(username,password);
}
}
我们再启动类那里添加扫描包
@MapperScan("com.example.demo.dao")
我们现在来测试一下功能,运行以后,我报错了!因为我在数据库里面没有创建相应的表
虽然springboot有插件可以自动生成,但是我们现在初入,所以还是慢慢来
在数据库里面新建一个表,字段与类里面一一对应
然后我们运行项目
浏览器输入http://localhost:8080/index 进入index.html页面
输入用户名与密码点击提交
我们看一下后台与数据库
到这里前端后台数据库都发生了数据的交互!
我的项目结构如下
IndexControl代码如下
package com.example.demo.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexControl {
@RequestMapping("/index")
public String index(){
return "index";
}
}
UserControl代码
package com.example.demo.control;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Scope("prototype")
@RequestMapping("/user")
public class UserControl {
@Autowired
private UserService us;
@RequestMapping("/query")
public List<User> query() throws Exception{
return us.query();
}
@RequestMapping("/insert")
public int insert(@RequestParam(value = "username") String username,@RequestParam(value = "password") String password) throws Exception{
System.out.println(username);
System.out.println(password);
return us.insert(username,password);
}
}
UserDao
package com.example.demo.dao;
import com.example.demo.model.User;
import java.util.List;
public interface UserDao {
//查询功能
List<User> query();
// 插入功能
int insert(User user);
}
User
package com.example.demo.model;
public class User {
private int id;
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
UserService
package com.example.demo.service;
import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserDao userdao;
public List<User> query(){
return userdao.query();
}
public int insert(String username,String password){
User u=new User();
u.setUsername(username);
u.setPassword(password);
return userdao.insert(u);
}
}
DemoApplication
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
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="com.example.demo.dao.UserDao">
<resultMap id="BaseResultMap" type="com.example.demo.model.User">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id, username,password
</sql>
<select id="query" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user
</select>
<insert id="insert" parameterType="com.example.demo.model.User">
insert into user (id, username,password
)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
</insert>
</mapper>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/user/insert">
<input name="username" type="text" placeholder="请输入用户名"/>
<input name="password" type="password" />
<input type="submit" value="注册">
</form>
</body>
</html>
application.yml
spring:
thymeleaf:
prefix: classpath:/templates/
datasource:
url: jdbc:mysql://localhost:3306/app?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml #对应mapper映射xml文件所在路径
type-aliases-package: cn.wbnull.springbootdemo.model #对应实体类路径
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 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.6.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--数据库配置-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</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>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<!-- mybatis代码生成插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
<!--添加mybatis generator maven插件-->
<!--添加插件-->
</plugins>
</build>
</project>