SpringBoot,从项目创建到简单实战
博客介绍: 简单SpringBoot,从创建项目,配置application.yml,到数据查询,templates数据视图传输
一、idea创建SpringBoot项目
项目结构预览
注意事项,避免报错:执行完之后找到启动类,如作者定义的Uyi31Application.java类,可以点击右侧绿标运行测试,没报错,代表创建成功
另外说明:springboot不用配置Tomcat
二、项目代码
项目结构:且注意添加@MapperScan("*")
application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone = GMT
username: root
password: "0504"
driverClassName: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource #Druid连接池
profiles:
active: dev #引用application-dev.properties文件
server:
port: 8031 #端口
session-timeout: 30
tomcat.max-threads: 100
tomcat.uri-encoding: UTF-8
mybatis:
# 指定别名设置的包为所有pojo
type-aliases-package: com.uyi31.pojo
configuration:
map-underscore-to-camel-case: true # 驼峰命名规范
# mapper-locations: classpath:com/uyi31/mapper/*.xml
application-dev.properties
spring.thymeleaf.prefix= classpath:/templates/
spring.thymeleaf.suffix: .html
spring.thymeleaf.check-template-location= true
#check-tempate-location: 检查模板路径是否存在
spring.thymeleaf.cache=false
#cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。
spring.thymeleaf.encoding= UTF-8
spring.thymeleaf.content-type= text/html
spring.thymeleaf.mode=HTML5
#扫描pojo下的类,并且aliases
mybatis.type-aliases-package=com.uyi31.pojo
uyi31.name=userTom
uyi31.age=22
passwword=liunianyanhuo
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.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.uyi31</groupId>
<artifactId>demo-blog</artifactId>
<version>demoVersion</version>
<name>uyi31</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
</properties>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- Spring Boot LOG 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!-- Spring Boot AOP 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!-- 数据库连接池 -->
<!-- <dependency>-->
<!-- <groupId>c3p0</groupId>-->
<!-- <artifactId>c3p0</artifactId>-->
<!-- <version>0.9.1.2</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!--spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
</dependency>
<!--apache公共辅助包括文件上传下载相关等五个) -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
<!-- excel导入 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>
<!--thymeleaf配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--@Date.lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<!-- 解决mapper绑定异常 -->
<resource>
<directory>src/main/java</directory>
<includes>
<!--可能会有人习惯将*.xml文件放在java目录下-->
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!-- 解决未找到数据源等配置文件异常 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.xml</include>
<include>**/*.html</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
UserController.java
package com.uyi31.controller;
import com.uyi31.pojo.User;
import com.uyi31.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;
/**
* @Description: UserController类中实现数据的增删查改
* @author: uyi31
* @date: 2022/10/14
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
/* 查询单条数据:
没有配置视图路径,会直接在web页面中显示内容*/
@RequestMapping("/select")
public String selectAll1() {
Map<String,Object> map= userService.selectUser();
return map.toString();
}
/*获取数据库数据显示在web网页上*/
@RequestMapping("/selectAll")
public ModelAndView selectAll2(ModelAndView mav) {
mav.setViewName("listUser");
List<User> uList= userService.selectUserAll();
mav.addObject("uList",uList);
return mav;
}
/*添加数据*/
@RequestMapping("/insert")
public String insertValue(){
User user = new User(3,"userTom","pwd1234","51dsada2");
int count=userService.insertValue(user);
if(count>0){
return "执行成功";
}else {
return "执行失败";
}
}
/*删除数据*/
@RequestMapping("/delete")
public String deleteValue(){
User user = new User(3);
int count=userService.deleteValue(user);
if(count>0){
return "执行成功";
}else {
return "执行失败";
}
}
/*删除数据*/
@RequestMapping("/update")
public String updateValue(){
User user = new User(4,"123","456","789");
int count=userService.updateValue(user);
if(count>0){
return "执行成功";
}else {
return "执行失败";
}
}
}
ThymeleafController.java
package com.uyi31.controller;
import com.uyi31.pojo.User;
import com.uyi31.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.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @Description: Thymeleaf实现视图跳转
* @Param: [mv]
* @return: org.springframework.web.servlet.ModelAndView
* @author: uyi31
* @date: 2022/10/14
*/
@Controller
@RequestMapping("/thyme")
public class ThymeleafController {
@Autowired
private UserService userService;
@RequestMapping(value = "/thymetest")
public ModelAndView ThymeleafTest(ModelAndView mv) {
/*视图会在application-dev.properties中配置,路径自动拼接查找:/templates/ThymeleafTest.html */
mv.setViewName("ThymeleafTest");
mv.addObject("msg","欢迎使用Thymeleaf!");
return mv;
}
@RequestMapping("/setValue")
public ModelAndView selectAll1(ModelAndView model) {
model.setViewName("index");
/*设置User数据*/
User user = new User(2,"dadsad","dasdag","rq3413");
model.addObject("user",user);
/*设置List数据*/
ArrayList<String> strList = new ArrayList<>();
strList.add("asdasdfa");
strList.add("78asd88d");
strList.add("dsad78sa");
model.addObject("strList",strList);
/*设置map数据*/
HashMap hMap = new HashMap();
hMap.put("key1","value1");
hMap.put("key3","value3");
hMap.put("key2","value2");
model.addObject("hMap",hMap);
return model;
}
@RequestMapping("/home")
public ModelAndView homeTest(ModelAndView modelAndView){
modelAndView.setViewName("home");
return modelAndView;
}
}
User.java
package com.uyi31.pojo;
import org.apache.ibatis.type.Alias;
/*
get,set方法。
@Data
有参构造方法
@AllArgsConstructor
无参构造方法
@NoArgsConstructor
*/
@Alias("user")
public class User {
private Integer uid;
private String uname;
private String upassword;
private String uemail;
public User() {
}
public User(Integer uid) {
this.uid = uid;
}
public User(Integer uid, String uname, String upassword, String uemail) {
this.uid = uid;
this.uname = uname;
this.upassword = upassword;
this.uemail = uemail;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
public String getUemail() {
return uemail;
}
public void setUemail(String uemail) {
this.uemail = uemail;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", upassword='" + upassword + '\'' +
", uemail=" + uemail +
'}';
}
}
UserService.java
package com.uyi31.service;
import com.uyi31.pojo.User;
import java.util.List;
import java.util.Map;
public interface UserService {
Map<String , Object> selectUser();
int insertValue(User user);
int deleteValue(User user);
List<User> selectUserAll();
int updateValue(User user);
}
UserServiceImpl.java
package com.uyi31.service.impl;
import com.uyi31.mapper.UserMapper;
import com.uyi31.pojo.User;
import com.uyi31.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class UserServiceImpl implements UserService {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private UserMapper userMapper;
public UserServiceImpl(){
}
/*查询用户*/
@Override
public Map<String , Object> selectUser(){
Map<String , Object> map= userMapper.selectUser();
return map;
}
@Override
public List<User> selectUserAll() {
List<User> uList=userMapper.selectUserAll();
return uList;
}
/*添加数据*/
@Override
public int insertValue(User user) {
int count=userMapper.insertUser(user);
return count;
}
/*删除数据*/
@Override
public int deleteValue(User user) {
int count=userMapper.deleteUser(user);
return count;
}
/*改*/
@Override
public int updateValue(User user) {
return userMapper.updateUser(user);
}
}
UserMapper.java
package com.uyi31.mapper;
import com.uyi31.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface UserMapper {
/*查询用户*/
Map<String , Object> selectUser();
@Select("select * from untitled")
List<User> selectUserAll();
/*插入数据*/
int insertUser(@Param("user") User user);
/*删除*/
int deleteUser(@Param("user") User user);
/*改*/
int updateUser(@Param("user") User user);
}
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.uyi31.mapper.UserMapper">
<!-- 用户查询,单条数据 -->
<select id="selectUser" resultType="HashMap">
select * from untitled where uid=1
</select>
<!--这里使用的user,是自定义pojo中已经建好的User.java类,对象为user-->
<insert id="insertUser" parameterType="user">
insert into untitled(uname,upassword,uemail) values(#{user.uname},#{user.upassword},#{user.uemail})
</insert>
<delete id="deleteUser" parameterType="user">
delete from untitled where uid = #{user.uid}
</delete>
<update id="updateUser" parameterType="user">
update untitled set uname=#{user.uname},upassword=#{user.upassword},uemail=#{user.uemail} where uid=#{user.uid}
</update>
</mapper>
home.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h2>消息表达</h2>
<table bgcolor="#ffe4c4" border="1">
<tr>
<td>name:</td>
<td th:text="#{uyi31.name}"></td>
</tr>
<tr>
<td>age:</td>
<td th:text="#{uyi31.age}"></td>
</tr>
<tr>
<td>upwd:</td>
<td th:text="#{passwword}"></td>
</tr>
</table>
</body>
</html>
index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<title>index.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" th:href="@{index.css}">
<script type="text/javascript" th:src="@{index.js}"></script>
</head>
<body>
<h2>index.html-JavaBean-map</h2>
<table bgcolor="#ffff" border="0">
<tr>
<td>name:</td>
<td th:text="${user.uname}"></td>
</tr>
<tr>
<td>emial:</td>
<td th:text="${user['uemail']}"></td>
</tr>
<tr>
<td>upwd:</td>
<td th:text="${user.getUpassword()}"></td>
</tr>
</table>
<h2>List</h2>
<table bgcolor="#ffe4c4" border="1">
<tr th:each="item:${strList}">
<td th:text="${item}"></td>
</tr>
</table>
<h2>Map-指定取值</h2>
<table bgcolor="#8fbc8f" border="1">
<tr>
<td>valueRes1:</td>
<td th:text="${hMap.get('key1')}"></td>
</tr>
<tr>
<td>valueRes2:</td>
<td th:text="${hMap['key2']}"></td>
</tr>
</table>
<div th:object="${hMap}">
<p>Name: <span th:text="*{key1}">赛</span>.</p>
<p>Age: <span th:text="*{key2}">18</span>.</p>
<p>Detail: <span th:text="*{key3}">好好学习</span>.</p>
</div>
</body>
</html>
listUser.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8">
<title>listUser</title>
</head>
<body>
<h1>遍历uList数据</h1>
<table>
<tr th:each="item:${uList}">
<td th:text="${item}"></td>
</tr>
</table>
</body>
</html>
ThymeleafTest.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<title>ThymeleafTest.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" th:href="@{index.css}">
<script type="text/javascript" th:src="@{index.js}"></script>
</head>
<body>
<h5 th:text="'Hello,' + ${msg}"></h5>
<div th:text="${msg}">这是数据渲染实例</div>
<a th:href="@{select}">index.html超链接</a>
</body>
</html>
创建sql -- mysql5.7
show
声明:资源免费下载
作者网上自学、尝试与总结…