创建Springboot项目

yeyfdH.png

创建项目的文件结构以及jdk的版本

yeyby8.png


选择项目所需依赖

ye69S0.png


修改pom.xml文件

添加mybatis-generator 插件

mybatis-generator-maven-plugin mybatis比较官方的代码生成器,生成do,mapper,mapper.xml且生成大量mybatis动态sql

<build>
<!--spring自带插件-->
<finalName>demo-0.0.1-SNAPSHOT</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis generator插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generator-config.xml</configurationFile>
</configuration>
<!--mybatis generator插件需要的mysql driver-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

yE1jv8.png

整个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>
<!--springWeb的集成-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 这里添加exclusions -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<!-- 此处删掉
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
-->


<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
相当于compile,但是打包阶段做了exclude操作-->
<scope>provided</scope>
</dependency>


<!--mybatis生成工具-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<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>
<!--spring自带插件-->
<finalName>demo-0.0.1-SNAPSHOT</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis generator插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generator-config.xml</configurationFile>
</configuration>
<!--mybatis generator插件需要的mysql driver-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>


配置GeneratorConfig.xml文件

(官方文档链接:http://mybatis.org/generator/configreference/xmlconfig.html

自己配置的generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="MySQL2Tables" targetRuntime="MyBatis3">

<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="addRemarkComments" value="true" />
</commentGenerator>

<!--数据源-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/medicaltarget?serverTimezone=UTC&amp;characterEncoding=UTF-8"
userId="root"
password="tw900516">
<!--避免生成不必要的类-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>

<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<javaModelGenerator targetPackage="com.atguigu.Entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>

<!-- <sqlMapGenerator targetPackage="com.example.demo.mapper" -->
<!-- targetProject="src/main/java"> -->
<!-- <property name="enableSubPackages" value="false" /> -->
<!-- </sqlMapGenerator> -->


<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.atguigu.Mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>


<table tableName="user"
domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 是否使用真实字段名"-->
</table>
</context>
</generatorConfiguration>

数据库简建表,配置

yEYGYd.png

建一个用户名以及密码(明文)的表

根据数据库端口号,用户密码来修改上述的generatorConfig.xml文件

修改Springboot配置文件(appliction.yml)

项目目录如下:

yEYXnK.png

server:
port: 8080
spring:
#数据源
datasource:
url: jdbc:mysql://localhost:3306/medicaltarget?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
username:
password:
driver-class-name: com.mysql.cj.jdbc.Driver

#mybatis的相关配置
mybatis:
#mapper配置文件
type-aliases-package: com.atguigu.Enity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true

由于本地安装的是Mysql8.0

注:若安装的是旧版本的Mysql,需要将url修改

编写相关Entity(实体类),Service,Controller,Mapper

项目目录结构如下:

yEN0iQ.png

编写过程可以参考b站尚学堂Springboot整合Mybatis

运用mybatis-generator插件进行自动生成

点击右边Maven选项中Plugins插件的Mybatis-generator(需根据表来配置好generatorConfig.xml才能进行生成)

yVvsDe.png

自动生成好Entity和Mapper类

yVv1jU.png

User

package com.atguigu.Entity;

public class User {
private String userId;

private String userSecret;

public User(String userId, String userSecret) {
this.userId = userId;
this.userSecret = userSecret;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUserSecret() {
return userSecret;
}

public void setUserSecret(String userSecret) {
this.userSecret = userSecret;
}
}

UserMapper:

package com.atguigu.Mapper;

import com.atguigu.Entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;

public interface UserMapper {
@Insert({
"insert into user (user_id, user_secret)",
"values (#{userId,jdbcType=VARCHAR}, #{userSecret,jdbcType=VARCHAR})"
})
int insert(User record);

@InsertProvider(type=UserSqlProvider.class, method="insertSelective")
int insertSelective(User record);
}

UserSqlProvider:

package com.atguigu.Mapper;

import com.atguigu.Entity.User;
import org.apache.ibatis.jdbc.SQL;

public class UserSqlProvider {
public String insertSelective(User record) {
SQL sql = new SQL();
sql.INSERT_INTO("user");

if (record.getUserId() != null) {
sql.VALUES("user_id", "#{userId,jdbcType=VARCHAR}");
}

if (record.getUserSecret() != null) {
sql.VALUES("user_secret", "#{userSecret,jdbcType=VARCHAR}");
}

return sql.toString();
}
}

根据自己需求编写业务层Service和Controller

以下是用户注册的Conrtroller代码

LogginController:

package com.atguigu.controller;

import com.atguigu.Entity.User;
import com.atguigu.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* @author shkstart
* @create 2021-01-2704
*/

@Controller
public class LoginController {
@Autowired
private UserService userService;

/**
* test 8080
*
* @return
*
@RequestMapping("/hello")
public String start() {
return "hello";
}
*
**/

@RequestMapping("/Regist")
public String Regist(){
return "Regist.html";
}


@RequestMapping("/Regist_user")
public String hivue(@RequestParam(value = "name") String name,
@RequestParam(value = "pwd") String password){

System.out.println(name);
System.out.println(password);
User user = new User(name, password);
userService.addUser(user);

return "regist successs";
}
// public String addUser(User userService){
// this.userService.addUser(userService);
// return "ok";
// }

}

页面跳转HtmlController

package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* @author shkstart
* @create 2021-01-28 10:48
*/

@Controller
public class HtmlController {
@RequestMapping("/")
public String index() {
return "Loggin.html";
}



}

Service

包含接口和实现类

UserService(接口)

package com.atguigu.Service;

import com.atguigu.Entity.User;

/**
* @author shkstart
* @create 2021-01-28 10:42
*/


public interface UserService {
void addUser(User user);
}

UserServiceImpl(接口实现类)

package com.atguigu.Service.Impl;

import com.atguigu.Entity.User;
import com.atguigu.Mapper.UserMapper;
import com.atguigu.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author shkstart
* @create 2021-01-28 10:43
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public void addUser(User user) {
this.userMapper.insert(user);
}
}

在启动类加入注解MapperScan

yZSWCR.png


前端相关代码(只可以添加用户(注册功能))

Loggin.html(需要自己引入Vue.js和axios)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<style>
.table{
width: 98.5%;
height: 95%;
margin: 10px auto;
border: 2px solid gray;
border-radius: 10px;

position: absolute;
background-image:url("background.jfif") ;
background-repeat: no-repeat;
background-size: cover;
}

.boxID{
background-color: #fff200;
border-radius: 3px;
border: 1px solid #dbca5e;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}
.boxID:hover{
background-color: #fffc7a;
border: 2px solid orange;
transform: scale(1.02);
}
.table2{
position: absolute;
top: 25%;
right: 13%;
border-radius: 5px;
border: 5px groove #b9f3e7;
}
.classDown{
color: #5051ff;
position: absolute;
top: 50%;
left: 15%;
font-size: 70px;
font-weight: bolder;
}
.classUp{
color: #5051ff;
position: absolute;
top: 30%;
left: 10%;
font-size: 70px;
font-weight: bolder;
}
.classUpchange{
color: #0003ff;
font-size: 75px;
}
</style>
<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<body>

<div class="table" id="table">
<div id="text">
<span :class="{classUp: hasClassUp ,classUpchange : haschange}">医学文本数据集
</span>
<span :class="{classDown: hasClassDown,classUpchange : haschange}">标 注 工 具
</span>
</div>

<table bgcolor="#faebd7" align="right"
width="280px" height="300px"
border="1" cellpadding="5px"
class="table2" cellspacing="0px" >
<tr> <td >登录入口</td> </tr>
<tr> <td>用户ID: &nbsp; &nbsp; <input type="text" name="登录ID" class="boxID" v-model="uid" ></td></tr>
<tr> <td>密码: &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
<input type="password" v-model="passWord" class="boxID" name="登录密码" value="">
</td> </tr>
<tr> <td height="20px" ><input align="right" width="100px" type = "button" value="登录" name="登录信息">
<input align="right" width="100px" type = "button" value="注册"
onclick="window.location.href = 'Regist.html'"
name="登录信息"></td>
</tr>
</table>
</div>
</body>
<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<script type="text/javascript" >
const Vm = new Vue({
el: '#table',
data:{
hasClassUp :true,
haschange : false,
hasClassDown : true,
},
mounted () {
setInterval(()=>{
this.haschange = !this.haschange
},1000)
}
})
</script>

</html>

Regist.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<style>
input[type]{
border: 1px solid darkorange;
background: white;
}
#button{
border: 10px solid orange;
width: 200px;
box-shadow:0px 4px 5px #666;
background: orange;
color: white;
}

</style>
<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<body>

<div id="Regist">
<form>
<h3>用户注册</h3>
<hr>
<p>用户名:<input type="text" id="name" v-model="name" placeholder="请填写用户名" required="required"
onblur="name1()" ><br><span id="tel"></span></p>
<p>密码:<input id="paswd" type="password" placeholder="请填写密码"
onblur="pwd1()" required="required" onfocus="pwd2()" v-model="pwd"><br><span id="pw"></span></p>

<p>邮箱:<input type="email" placeholder="请填写邮箱" required="required"></p>
<p><input type="checkbox" required="required">我已阅读注册手册</p>
<p><button id="button" @click=onSubmit($event) >注册</button></p>
</form>
</div>

</body>

<script type="text/javascript">
const Vm = new Vue({
el: '#Regist',
data: {
name:'',
pwd: '',
},

methods:{

onSubmit:function (event) {
axios({
method: "post",
url: "http://localhost:8080/Regist_user",
params:{
name: this.name,
pwd: this.pwd,
},
withCredentials:true,
}).then((res)=>{
console.log(res.data);
alert("success")
// if (res.data ) {
//
// window.location.href = "LibrarySystem.html";
// }else{
//
// alert("输入错误 请重新输入");
// }
//
//
//
// console.log(res.data);
}).catch(err => {
console.log('error');
});
}
}
})
</script>
<script type="text/javascript">
function name1(){
var name=document.getElementById("name").value;
if(name.length==""){
document.getElementById("tel").innerHTML="用户名不能为空"
document.getElementById("tel").style.color="red";
}else{
document.getElementById("tel").innerHTML="√"
document.getElementById("tel").style.color="green";
}
}
// function name2(){
// document.getElementById("tel").innerHTML="请填写用户名"
// document.getElementById("tel").style.color="#999";
// }
function pwd2(){
document.getElementById("pw").innerHTML="请填写6-12位的密码"
document.getElementById("pw").style.color="#999";

}
function pwd1(){
p=document.getElementById("paswd").value;

if(p.length>=6&&p.length<=20)
{
document.getElementById("pw").innerHTML="√"
document.getElementById("pw").style.color="green";
}else{
document.getElementById("pw").innerHTML="格式错误,请输入6-20位"
document.getElementById("pw").style.color="red";
}
}
function validate(){
var qpw=document.getElementById("paswd").value;
var qpw2=document.getElementById("paswd2").value;

if(qpw==qpw2 && p.length>=6&&p.length<=20){
document.getElementById("qpwtx").innerHTML="<font color='green'>√</font>";
document.getElementById("button").disabled = false;
}
else {
document.getElementById("qpwtx").innerHTML="<font color='red'>两次密码不相同或者格式错误</font>";
document.getElementById("button").disabled = true;
}
}

</script>

</html>

数据库建表

yZpszt.png