创建SpringBoot项目(使用阿里云代理)
第一个SpringBoot项目
目录
1.更改服务器URL
2. 点击下一步编辑
3.勾选三项,点击创建编辑
4.等待同步完成(将近5分钟)在等待的过程中创建数据库编辑
6.mysql创建class3数据库
7.引入mybatis mysql依赖包
8.创建包(如图所示)controller dao pojo service utils 编辑
9.在resources中创建mappers文件夹
10.application.properties配置文件中写入,更改连接地址、用户名和密码
11.创建实体类
12.dao层
13. service层
14.controller层
15.mappers sql语句
16.templates 三个页面
17.运行成功
https://start.aliyun.com
1.更改服务器URL
2. 点击下一步
3.勾选三项,点击创建
4.等待同步完成(将近5分钟)在等待的过程中创建数据库
6.mysql创建class3数据库
创建person、idcard表
sql语句:
create table idcard(
id int not null primary key AUTO_INCREMENT,
stuid varchar(15),
classname varchar(15)
);
create table person(
id int not null primary key AUTO_INCREMENT,
age int,
name varchar(15),
idcard int not null,
foreign key(idcard) references idcard(id)
);
INSERT INTO idcard VALUES(1,2,"一班"),(2,3,"二班"),(3,4,"三班");
INSERT INTO person VALUES(1,18,"小红",1),(2,19,"小华",2),(3,20,"小明",3);
7.引入mybatis mysql依赖包
mybatis mysql依赖包:
<!-- 引入mybaits mysql依赖包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
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>
<groupId>hnucm.example</groupId>
<artifactId>SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot</name>
<description>SpringBoot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 引入mybaits mysql依赖包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.hnucm.springboot.Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
8.创建包(如图所示)controller dao pojo service utils
9.在resources中创建mappers文件夹
10.application.properties配置文件中写入,更改连接地址、用户名和密码
spring.application.name=SpringBoot
server.port=8080
# 数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/class3?characterEncoding=utf8&serverTimezone=UTC
# 用户名和密码
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.name=defaultDataSource
# mybatis配置 sql写到配置文件
# 实体类包
mybatis.type-aliases-package=com.hnucm.springboot.pojo
# sql配置文件xml路径
mybatis.mapper-locations=classpath:mappers/*.xml
# 日志输出路径
logging.level.com.hnucm.springboot=debug
11.创建实体类
package com.hnucm.springboot.pojo;
import lombok.Data;
@Data
public class Idcard {
private int id;
private String stuid;
private String classname;
}
package com.hnucm.springboot.pojo;
import lombok.Data;
@Data
public class Person {
private int id;
private int age;
private String name;
private Idcard idcardid;
}
12.dao层
package com.hnucm.springboot.dao;
import com.hnucm.springboot.pojo.Idcard;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface IdcardMapper {
public Idcard findIdcardById(Integer id);
public int addIdcard(Idcard idcard);
public int deleteIdcardById(Integer id);
}
package com.hnucm.springboot.dao;
import com.hnucm.springboot.pojo.Person;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface PersonMapper {
public List<Person> findAllPersonIdcard();
public List<Person> findAllPersonIdcard2();
public List<Person> findAllPerson();
public int deletePersonById(Integer id);
public int addPerson(Person person);
public int updatePerson(Person person);
}
13. service层
package com.hnucm.springboot.service;
import com.hnucm.springboot.pojo.Person;
import java.util.List;
public interface PersonService {
public List<Person> findAllPerson();
public int deletePersonById(Integer id,Integer idcardid);
public int addPerson(Person person);
public int updatePerson(Person person);
}
package com.hnucm.springboot.service;
import com.hnucm.springboot.dao.IdcardMapper;
import com.hnucm.springboot.dao.PersonMapper;
import com.hnucm.springboot.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class PersonServiceImpl implements PersonService{
@Autowired
PersonMapper personMapper;
@Autowired
IdcardMapper idcardMapper;
// @Override
// public List<Person> findAllPerson() {
// return personMapper.findAllPerson();
// }
@Transactional
@Override
public int deletePersonById(Integer id,Integer idcardid) {
idcardMapper.deleteIdcardById(idcardid);
return personMapper.deletePersonById(id);
}
@Override
public int addPerson(Person person) {
int result1= idcardMapper.addIdcard(person.getIdcardid());
int result2= personMapper.addPerson(person);
if(result2>0&&result1>0){
return result1;}
return 0;
}
@Override
public int updatePerson(Person person) {
return personMapper.updatePerson(person);
}
@Override
public List<Person> findAllPerson(){
return personMapper.findAllPersonIdcard2();
}
}
14.controller层
package com.hnucm.springboot.controller;
import com.hnucm.springboot.pojo.Person;
import com.hnucm.springboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping("personlist")
public String personlist(Model model){
model.addAttribute("personlist",personService.findAllPerson());
return "personlist.html";
}
@RequestMapping("deletepersonbyid")
public String deletepersonbyid(int id,int idcardid){
personService.deletePersonById(id,idcardid);
return "redirect:/personlist";
}
@RequestMapping("addPerson")
public String addPerson(){
return "addPerson.html";
}
@RequestMapping("addPersoncommit")
public String addPersoncommit(Person person){
personService.addPerson(person);
return "redirect:/personlist";
}
@RequestMapping("updateperson")
public String addPersoncommit(Model model,Person person){
model.addAttribute("id",person.getId());
model.addAttribute("name",person.getName());
model.addAttribute("age",person.getAge());
return "updateperson.html";
}
@RequestMapping("updatePersoncommit")
public String updatePersoncommit(Person person){
personService.updatePerson(person);
return "redirect:/personlist";
}
}
15.mappers sql语句
<?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.hnucm.springboot.dao.IdcardMapper">
<select id="findIdcardById"
resultType="com.hnucm.springboot.pojo.Idcard"
parameterType="Integer">
select * from idcard where id =#{id}
</select>
<insert id="addIdcard" parameterType="com.hnucm.springboot.pojo.Idcard"
useGeneratedKeys="true"
keyProperty="id">
insert into idcard(stuid,classname) values (#{stuid},#{classname})
</insert>
<delete id="deleteIdcardById" parameterType="Integer">
delete from idcard where id =#{id}
</delete>
</mapper>
<?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.hnucm.springboot.dao.PersonMapper">
<select id="findAllPerson" resultType="com.hnucm.springboot.pojo.Person">
select * from person
</select>
<delete id="deletePersonById" parameterType="Integer">
delete from person where id=#{id}
</delete>
<insert id="addPerson" parameterType="com.hnucm.springboot.pojo.Person">
insert into person(name,age,idcardid) values (#{name},#{age},#{idcardid.id})
</insert>
<update id="updatePerson" parameterType="com.hnucm.springboot.pojo.Person">
update person
set name=#{name},age=#{age}
where id=#{id}
</update>
<select id="findAllPersonIdcard2" resultMap="personIdcard2">
select * from person
</select>
<resultMap id="personIdcard2" type="com.hnucm.springboot.pojo.Person">
<id column="id" property="id"></id>
<result column="age" property="age"></result>
<result column="name" property="name"></result>
<association property="idcardid"
javaType="com.hnucm.springboot.pojo.Idcard"
column="idcardid"
select="com.hnucm.springboot.dao.IdcardMapper.findIdcardById">
</association>
</resultMap>
</mapper>
16.templates 三个页面
addPerson.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>增加用户</h1>
<form th:action="@{/addPersoncommit}" method="post">
<p>用户名:<input type="text" name="name"><p>
<p> 年龄:<input type="text" name="age"><p>
<p> 学号:<input type="text" name="idcardid.stuid"><p>
<p> 班级:<input type="text" name="idcardid.classname"><p>
<p><input type="submit" value="提交"><p>
</form>
</body>
</html>
personlist.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>用户id</td>
<td>用户姓名</td>
<td>用户年龄</td>
<td>学号</td>
<td>班级</td>
<td>删除</td>
<td>更新</td>
</tr>
<tr th:each="person:${personlist}">
<td th:text="${person.id}"></td>
<td th:text="${person.name}"></td>
<td th:text="${person.age}"></td>
<td th:text="${person.idcardid.stuid}"></td>
<td th:text="${person.idcardid.classname}"></td>
<td>
<a th:href="@{deletepersonbyid(id=${person.id},idcardid=${person.idcardid.id})}">删除用户</a>
</td>
<td>
<a th:href="@{updateperson(id=${person.id},age=${person.age},name=${person.name})}">更新用户</a>
</td>
</tr>
</table>
</body>
</html>
updateperson.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>更新用户</h1>
<form th:action="@{/updatePersoncommit}" method="post">
<p>id:<input type="text" name="id" th:value="${id}"><p>
<p>用户名:<input type="text" name="name" th:value="${name}"><p>
<p> 年龄:<input type="text" name="age" th:value="${age}"><p>
<p><input type="submit" value="提交"><p>
</form>
</body>
</html>
17.运行成功