一、pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_annotation</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring_annotation Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>6.0.9</springframework.version>
<jackson.version>2.15.1</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<!-- spring core container -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework</groupId>-->
<!-- <artifactId>spring-context</artifactId>-->
<!-- <version>${springframework.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
<!-- spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.19</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>
<!-- bean-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.0.Final</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.2</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
<scope>runtime</scope>
</dependency>
<!--Thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring6</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.4</version>
</dependency>
<!--日志 start-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>ssm-spring6</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
二、配置文件
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm
jdbc.username=root
jdbc.password=root
#初始连接数量
initialSize=5
#最大连接数量
maxActive=10
#最大等待时间
maxWait=2000
三、实体类
package com.song.ssm.pojo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
@Data
public class Person implements Serializable {
private long id;
private String username;
private String password;
private Date birthday;
private String mobile;
private Integer sex;
private String address;
}
四、配置类
4.1 数据源
package com.song.ssm.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
@PropertySource("classpath:db.properties")
public class JdbcConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driverClassName}")
private String driver;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${maxActive}")
private Integer maxActive;
@Value("${maxWait}")
private long maxWait;
@Value("${initialSize}")
private Integer initSize;
@Bean
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setUsername(username);
ds.setPassword(password);
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setMaxActive(this.maxActive);
ds.setInitialSize(this.initSize);
ds.setMaxWait(this.maxWait);
return ds;
}
}
4.2 mybatis配置
package com.song.ssm.config;
import com.github.pagehelper.PageHelper;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean getSessionFactoryBean(@Autowired DataSource ds) {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(ds);
return sessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.song.ssm.mapper");
return mapperScannerConfigurer;
}
}
4.3 mvc
package com.song.ssm.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
@Configuration
@EnableWebMvc
@ComponentScan("com.song.ssm.controller")
public class MvcConfig {
}
4.4 Spring容器
package com.song.ssm.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
@Configuration
@Import({JdbcConfig.class, MyBatisConfig.class})
@ComponentScan("com.song.ssm.service")
public class SpringConfig {
}
4.5 Servlet初始化
package com.song.ssm.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
public class ServletInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{MvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
五、Mapper
package com.song.ssm.mapper;
import com.song.ssm.pojo.Person;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
public interface PersonMapper {
@Select("select * from t_person")
List<Person> findAll();
}
六、业务层
package com.song.ssm.service;
import com.song.ssm.pojo.Person;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
@Transactional
public interface PersonService {
List<Person> findAll();
}
package com.song.ssm.service.impl;
import com.song.ssm.mapper.PersonMapper;
import com.song.ssm.pojo.Person;
import com.song.ssm.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personMapper;
@Override
public List<Person> findAll() {
return this.personMapper.findAll();
}
}
七、控制器
package com.song.ssm.controller;
import com.song.ssm.pojo.Person;
import com.song.ssm.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author 宋伟宁
* @Date 2023/9/1
* @Version 1.0
**/
@RestController
public class HelloController {
@Autowired
private PersonService personService;
@GetMapping("/list")
public List<Person> findAll() {
return this.personService.findAll();
}
}
页面效果: