application.properties
或application.yml
中配置数据库连接信息,包括数据库 URL、用户名、密码、mybatis 的功能配置等。@MapperScan
注解,用于扫描和注册 Mapper 接口。<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.0.5version>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>3.0.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-3-starterartifactId>
<version>1.2.18version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.28version>
dependency>
dependencies>
server:
port: 80
servlet:
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
url: jdbc:mysql:///day01
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
configuration: # setting配置
auto-mapping-behavior: full
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
type-aliases-package: com.alex.pojo # 配置别名
mapper-locations: classpath:/mapper/*.xml # mapperxml位置
package com.alex.pojo;
public class User {
private String account ;
private String password ;
private Integer id ;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"account='" + account + '\'' +
", password='" + password + '\'' +
", id=" + id +
'}';
}
}
public interface UserMapper {
List<User> queryAll();
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.alex.mapper.UserMapper">
<select id="queryAll" resultType="user">
select * from users
select>
mapper>
伪代码,不添加业务接口!
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
@ResponseBody
public List<User> getUser(){
List<User> userList = userService.findList();
log.info("查询的user数据为:{}",userList);
return userList;
}
}
@Slf4j
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findList(){
List<User> users = userMapper.queryAll();
log.info("查询全部数据:{}",users);
return users;
}
}
@MapperScan("com.alex.mapper") //mapper接口扫描配置
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
@Transactional
public void update(){
User user = new User();
user.setId(1);
user.setPassword("test2");
user.setAccount("test2");
userMapper.update(user);
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
@Component
@Aspect
public class LogAdvice {
@Before("execution(* com..service.*.*(..))")
public void before(JoinPoint joinPoint){
System.out.println("LogAdvice.before");
System.out.println("joinPoint = " + joinPoint);
}
}
在 Spring Boot 项目中添加 spring-boot-maven-plugin 插件是为了支持将项目打包成可执行的可运行 jar 包。如果不添加 spring-boot-maven-plugin 插件配置,使用常规的 java -jar 命令来运行打包后的 Spring Boot 项目是无法找到应用程序的入口点,因此导致无法运行。
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
java -jar
命令用于在 Java 环境中执行可执行的 JAR 文件。下面是关于java -jar
命令的说明:命令格式:java -jar [选项] [参数] <jar文件名>
-D=
:设置系统属性,可以通过System.getProperty()
方法在应用程序中获取该属性值。例如:java -jar -Dserver.port=8080 myapp.jar
。-X
:设置 JVM 参数,例如内存大小、垃圾回收策略等。常用的选项包括:
-Xmx
:设置 JVM 的最大堆内存大小,例如 -Xmx512m
表示设置最大堆内存为 512MB。-Xms
:设置 JVM 的初始堆内存大小,例如 -Xms256m
表示设置初始堆内存为 256MB。-Dspring.profiles.active=
:指定 Spring Boot 的激活配置文件,可以通过application-.properties
或application-.yml
文件来加载相应的配置。例如:java -jar -Dspring.profiles.active=dev myapp.jar
。