开发工具基于IDEA + Maven + Navicat for MySQL
下载Maven后配置环境变量
MAVEN_HOME=upath
%JAVA_HOME%\bin
%MAVEN_HOME%\bin
创建Maven本地仓库,并在%MAVEN_HOME%\conf\setting.xml路径下修改
的路径为本地仓库路径
文件-设置-构建-构建工具-Maven
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.3.RELEASEversion>
<relativePath/>
parent>
<groupId>com.pratice.persondemogroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.45version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
使用Navicat for MySQL创建数据库springdemo
并使用该数据库创建persons
表
use springdemo;
create table persons(
id int(3) primary key not null auto_increment,
name varchar(50),
gender varchar(10),
age int(3)
);
完成数据库创建后,在我们的项目中创建对应的JavaBean对象
src-main-java-uProjectName在该目录下创建一个名为pojo的package
创建完成后在pojo包下创建一个名为Person的java类
public class Person {
private int id;
private String name;
private String gender;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
spring.datasource.url=uUrl
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=uPwd
数据访问层实现 接口+配置
使用注释语句实现数据库数据操作
@Mapper
public interface PersonMapper {
@Select("select * from persons")
List<Person> getAllPersons();
@Select("select * from persons where id=#{id}")
Person getPersonByID(int id);
@Insert("insert into persons(name,gender,age) values(#{name},#{gender},#{age})")
int addPerson(Person person);
@Update("update persons set name=#{name},gender=#{gender},age=#{age} where id=#{id}")
int updatePerson(Person person);
@Delete("delete from persons where id=#{id}")
int deletePersonByID(int id);
}
public interface PersonService {
List<Person> getAllPersons();
Person getPersonByID(int id);
int addPerson(Person person);
int updatePerson(Person person);
int deletePersonByID(int id);
}
Service接口实现类
Autowired注解使用控制反转,由Spring自动按类型注入bean对象
在Service层使用数据访问层mapper中的基本数据库操作组合实现业务逻辑操作。
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personMapper;
@Override
public List<Person> getAllPersons() {
return personMapper.getAllPersons();
}
@Override
public Person getPersonByID(int id) {
return personMapper.getPersonByID(id);
}
@Override
public int addPerson(Person person) {
return personMapper.addPerson(person);
}
@Override
public int updatePerson(Person person) {
return personMapper.updatePerson(person);
}
@Override
public int deletePersonByID(int id) {
return personMapper.deletePersonByID(id);
}
}
加载模板引擎查找所有记录
controller方法实现
@RequestMapping("/list")
public String getAllPersons(Map<String,Object> map){
List<Person> list=personService.getAllPersons();
map.put("persons", list);
return "person/list";
}
页面实现
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>人员信息表title>
head>
<body>
<a th:href="@{/add}">添加记录a>
<table border="1px">
<tr>
<td colspan="6" align="center">人员信息表td>
tr>
<tr>
<td>IDtd>
<td>姓名td>
<td>性别td>
<td>年龄td>
tr>
<tr th:each="person:${persons}">
<td th:text="${person.id}">IDtd>
<td th:text="${person.name}">姓名td>
<td th:text="${person.gender}">性别td>
<td th:text="${person.age}">年龄td>
tr>
table>
body>
html>
添加记录
controller方法实现
@GetMapping("/add")
public String addPerson(){
return "person/add";
}
@PostMapping("/add")
public String addPerson(Person person){
personService.addPerson(person);
return "redirect:/list";
}
页面实现
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加记录title>
head>
<body>
<form th:action="@{/add}" th:method="post">
<table border="1px">
<tr>
<td colspan="2" align="center">添加记录td>
tr>
<tr>
<td>姓名td>
<td>
<input type="text" name="name"/>
td>
tr>
<tr>
<td>性别td>
<td>
<select name="gender">
<option>请选择option>
<option value="男">男option>
<option value="女">女option>
select>
td>
tr>
<tr>
<td>年龄td>
<td>
<input type="text" name="age"/>
td>
tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="添加"/>
td>
tr>
table>
form>
body>
html>
修改记录
controller方法实现
@GetMapping("/update/{id}")
public String updatePerson(@PathVariable("id") int id,Map<String,Person> map){
Person person = personService.getPersonByID(id);
map.put("person", person);
return "person/update";
}
@PostMapping("/update")
public String updatePerson(Person person){
personService.updatePerson(person);
return "redirect:/list";
}
页面实现
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改记录title>
head>
<body>
<form th:action="@{/update}" th:method="post">
<input type="hidden" name="id" th:value="${person.id}"/>
<table border="1px">
<tr>
<td colspan="2" align="center">修改记录td>
tr>
<tr>
<td>IDtd>
<td th:text="${person.id}">td>
tr>
<tr>
<td>姓名td>
<td>
<input type="text" name="name" th:value="${person.name}"/>
td>
tr>
<tr>
<td>性别td>
<td>
<select name="gender">
<option value="男" th:selected="${person.gender}=='男'">男option>
<option value="女" th:selected="${person.gender}=='女'">女option>
select>
td>
tr>
<tr>
<td>年龄td>
<td>
<input type="text" name="age" th:value="${person.age}"/>
td>
tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="修改"/>
td>
tr>
table>
form>
body>
html>
删除记录
controller方法实现
@GetMapping("/delete/{id}")
public String deletePerson(@PathVariable("id") int id){
personService.deletePersonByID(id);
return "redirect:/list";
}
完整controller类代码
@Controller
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping("/list")
public String getAllPersons(Map<String,Object> map){
List<Person> list=personService.getAllPersons();
map.put("persons", list);
return "person/list";
}
@GetMapping("/add")
public String addPerson(){
return "person/add";
}
@PostMapping("/add")
public String addPerson(Person person){
personService.addPerson(person);
return "redirect:/list";
}
@GetMapping("/update/{id}")
public String updatePerson(@PathVariable("id") int id,Map<String,Person> map){
Person person = personService.getPersonByID(id);
map.put("person", person);
return "person/update";
}
@PostMapping("/update")
public String updatePerson(Person person){
personService.updatePerson(person);
return "redirect:/list";
}
@GetMapping("/delete/{id}")
public String deletePerson(@PathVariable("id") int id){
personService.deletePersonByID(id);
return "redirect:/list";
}
}
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>人员信息表title>
head>
<body>
<a th:href="@{/add}">添加记录a>
<table border="1px">
<tr>
<td colspan="6" align="center">人员信息表td>
tr>
<tr>
<td>IDtd>
<td>姓名td>
<td>性别td>
<td>年龄td>
<td>修改td>
<td>删除td>
tr>
<tr th:each="person:${persons}">
<td th:text="${person.id}">IDtd>
<td th:text="${person.name}">姓名td>
<td th:text="${person.gender}">性别td>
<td th:text="${person.age}">年龄td>
<td><a th:href="@{/update/}+${person.id}">修改a>td>
<td><a th:href="@{/delete/}+${person.id}" onclick="return confirm('确定要删除该记录吗?')">删除a>td>
tr>
table>
body>
html>
java -jar xxx.jar
便可以实现脱离开发环境运行