目录
■相关知识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
4.0.0
sxz.com
SpringBootStaduy001
0.0.1-SNAPSHOT
jar
SpringBootStaduy001
http://maven.apache.org
UTF-8
org.springframework.boot
spring-boot-starter-parent
2.3.10.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
mysql
mysql-connector-java
org.mybatis
mybatis
3.4.5
org.mybatis
mybatis-spring
2.0.0
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
org.apache.logging.log4j
log4j-core
org.projectlombok
lombok
junit
junit
test
org.springframework.boot
spring-boot-maven-plugin
true
■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
・注意点(配置文件)
4.0.0 sxz.com SpringBootStaduy0010.0.1-SNAPSHOT jar SpringBootStaduy001 http://maven.apache.org UTF-8 org.springframework.boot spring-boot-starter-parent2.3.10.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-thymeleafmysql mysql-connector-javaorg.mybatis mybatis3.4.5 org.mybatis mybatis-spring2.0.0 org.mybatis.spring.boot mybatis-spring-boot-starter2.0.1 org.apache.logging.log4j log4j-coreorg.projectlombok lombokjunit junittest org.springframework.boot spring-boot-maven-plugintrue
■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配置)
mysql mysql-connector-java
・通过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);
}
}
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配置
org.mybatis mybatis-spring2.0.0 org.mybatis.spring.boot mybatis-spring-boot-starter2.0.1
注意,只是对应上面第一个依赖,但是第二个也要写,不写会出如下的错误
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 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
org.projectlombok lombok
・扩展(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 findAll();
}
・标注【@Mapper】对应的Maven配置
org.mybatis mybatis3.4.5
・关联对应的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 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 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 findAll() {
return userRepository.findAll();
}
}
■xml代码
package com.sxz.test.one.repository;
import java.util.List;
import com.sxz.test.one.entity.User;
public interface UserRepository {
public List 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 findAll() {
return userRepository.findAll();
}
}
■xml代码
UserMapper.xml (关联代码【4.UserMapper】)
select * from login_user
----
■访问效果
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:devSpringBootHelloWorldtargetclasses started by LOGIN_USER_NAME in C:devSpringBootHelloWorld)
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.
---



