目录
■相关知识1(其他代码)
■相关知识2(补充说明)
■目录结构
■配置文件
?■POM
■application.properties
?注意点(配置文件)
■DB
■Java代码
1.HelloWorldMainApplication (启动类)
2.UserController? (控制类)
3.User
4.UserMapper.java (关联对应的DB操作相关的UserMapper.xml文件 【@Mapper】)
5.UserRepositoryImpl?(@Repository 层)
6.UserRepository
7.UserService? (@Service 层)
■xml代码
■访问效果
■DB数据
■启动Log
■相关知识1(其他代码)
SpringBoot + Thymeleaf 之 HelloWorld_sun0322-CSDN博客
■相关知识2(补充说明)
只是helloWorld,关于DB操作时的事物处理,并没有事物的具体使用。
(只是使用了一个标注(@Transactional),意思一下)
■目录结构
? ? ??
■配置文件
?■POM
<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.0</modelVersion>
<groupId>sxz.com</groupId>
<artifactId>SpringBootStaduy001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootStaduy001</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 画面,thymeleaf,需要使用的驱动类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 访问数据库,需要使用的驱动类 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 纯MyBatis使用的类(比如@Mapper) -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- MyBatis与Spring结合,使用到的类(比如@MapperScan) -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<!-- MyBatis与Spring结合,使用到的类(没有启动时会报错) -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
■application.properties
server.port=443
server.ssl.key-store: .keystore
server.ssl.key-store-password: tomcat
server.ssl.keyStoreType: JKS
server.ssl.keyAlias: myTomcat
# 画面 thymeleaf 配置
spring.thymeleaf.enabled:true
spring.thymeleaf.cache: false
spring.thymeleaf.mode: html
# 数据库连接配置
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/messageboard
spring.datasource.username: root
spring.datasource.password: root001
spring.datasource.sql-script-encoding: UTF-8
#mybatis的相关配置
mybatis.mapper-locations: classpath:mapper/*.xml
mybatis.type-aliases-package: com.sxz.test.one.entity
#开启驼峰命名
mybatis.configuration.map-underscore-to-camel-case: true
?注意点(配置文件)
? ????这一版springboot的配置文件,不支持阶梯式命名!!!
? ? ?数据库访问XML位置? (下面黄色标记(可自行定义文件夹结构))
?访问数据库,需要使用的驱动类(的Maven配置)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
??通过Spring的基类,自动配置数据源
DataSourceAutoConfiguration
?---
?---
■DB
■Java代码
1.HelloWorldMainApplication (启动类)
package com.sxz.test.one;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("com.sxz.test.one.mapper")
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
因为这里面有【@MapperScan("com.sxz.test.one.mapper")】,所以可以自动加载这个包下面的类
标注【@MapperScan】对应的Maven配置
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
注意,只是对应上面第一个依赖,但是第二个也要写,不写会出如下的错误
java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.mybatis.spring.annotation.MapperScan.factoryBean() ?? ?at java.lang.reflect.Method.getDefaultValue(Method.java:612) ~[na:1.8.0_191]
2.UserController? (控制类)
package com.sxz.test.one.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sxz.test.one.entity.User;
import com.sxz.test.one.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}
有两个?@RequestMapping("/XXX")
所以访问时,按照下面的地址访问。
https://10.10.10.194/user/findAll
3.User
package com.sxz.test.one.entity;
import java.io.Serializable;
import lombok.Data;
@Data
public class User implements Serializable {
private String luId;
private String luPass;
private String luPermission;
}
注意,要提前引入lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
?扩展(lombok安装)
Eclipse中,lombok安装_sun0322-CSDN博客
4.UserMapper.java (关联对应的DB操作相关的UserMapper.xml文件 【@Mapper】)
package com.sxz.test.one.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.sxz.test.one.entity.User;
@Mapper
public interface UserMapper {
public List<User> findAll();
}
?标注【@Mapper】对应的Maven配置
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
?关联对应的xml文件
5.UserRepositoryImpl?(@Repository 层)
package com.sxz.test.one.repository.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.sxz.test.one.entity.User;
import com.sxz.test.one.mapper.UserMapper;
import com.sxz.test.one.repository.UserRepository;
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
6.UserRepository
package com.sxz.test.one.repository;
import java.util.List;
import com.sxz.test.one.entity.User;
public interface UserRepository {
public List<User> findAll();
}
7.UserService? (@Service 层)
package com.sxz.test.one.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sxz.test.one.entity.User;
import com.sxz.test.one.repository.UserRepository;
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> findAll() {
return userRepository.findAll();
}
}
■xml代码
UserMapper.xml? (关联代码【4.UserMapper】)
<?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.sxz.test.one.mapper.UserMapper">
<select id="findAll" resultType="User">
select * from login_user
</select>
</mapper>
----
■访问效果
??????https://10.10.10.194/user/findAll
■DB数据
----
■启动Log
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.10.RELEASE)
2021-10-10 13:18:51.809 INFO 2236 --- [ main] c.s.test.one.HelloWorldMainApplication : Starting HelloWorldMainApplication on PC_NAME with PID 2236 (C:\dev\SpringBootHelloWorld\target\classes started by LOGIN_USER_NAME in C:\dev\SpringBootHelloWorld)
2021-10-10 13:18:51.813 INFO 2236 --- [ main] c.s.test.one.HelloWorldMainApplication : No active profile set, falling back to default profiles: default
2021-10-10 13:18:53.102 INFO 2236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 443 (https)
2021-10-10 13:18:53.112 INFO 2236 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-10 13:18:53.112 INFO 2236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-10-10 13:18:53.200 INFO 2236 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-10 13:18:53.200 INFO 2236 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1335 ms
2021-10-10 13:18:53.653 INFO 2236 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-10-10 13:18:53.738 INFO 2236 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2021-10-10 13:18:54.400 INFO 2236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 443 (https) with context path ''
2021-10-10 13:18:54.406 INFO 2236 --- [ main] c.s.test.one.HelloWorldMainApplication : Started HelloWorldMainApplication in 2.989 seconds (JVM running for 3.38)
2021-10-10 13:19:05.084 INFO 2236 --- [-nio-443-exec-9] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-10 13:19:05.084 INFO 2236 --- [-nio-443-exec-9] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-10 13:19:05.100 INFO 2236 --- [-nio-443-exec-9] o.s.web.servlet.DispatcherServlet : Completed initialization in 16 ms
2021-10-10 13:19:05.175 INFO 2236 --- [-nio-443-exec-9] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-10-10 13:19:05.534 INFO 2236 --- [-nio-443-exec-9] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
---
|