整合 Spring、SpringMVC、MyBatis。
-
-
-
org.projectlombok -
lombok -
1.18.22 -
-
-
org.slf4j -
slf4j-api -
1.7.32 -
-
-
ch.qos.logback -
logback-classic -
1.2.10 -
-
-
-
-
com.alibaba -
fastjson -
1.2.76 -
-
-
org.apache.commons -
commons-collections4 -
4.3 -
-
-
org.apache.commons -
commons-lang3 -
3.11 -
-
-
cn.hutool -
hutool-all -
5.7.22 -
-
-
com.fasterxml.jackson.core -
jackson-databind -
2.12.1 -
-
-
-
org.apache.logging.log4j -
log4j-web -
2.12.1 -
-
-
-
-
org.springframework -
spring-webmvc -
5.3.4 -
-
-
org.mybatis -
mybatis -
3.5.6 -
-
-
org.mybatis -
mybatis-spring -
2.0.6 -
-
-
mysql -
mysql-connector-java -
8.0.26 -
-
-
org.springframework -
spring-jdbc -
5.3.4 -
-
-
org.springframework -
spring-aop -
5.3.4 -
-
-
-
org.springframework -
spring-beans -
5.3.4 -
-
-
org.springframework -
spring-context -
5.3.4 -
-
-
org.springframework -
spring-core -
5.3.4 -
-
-
org.springframework -
spring-expression -
5.3.4 -
-
-
org.springframework -
spring-tx -
5.3.4 -
-
-
-
-
cglib -
cglib -
3.1 -
-
-
aopalliance -
aopalliance -
1.0 -
-
-
org.aspectj -
aspectjweaver -
1.9.19 -
-
-
-
-
-
-
src/main/java -
-
**/*.xml -
-
-
-
src/main/resources -
-
-
-
-
org.apache.maven.plugins -
maven-compiler-plugin -
3.10.1 -
-
8 -
8 -
-
-
- db.driver=com.mysql.cj.jdbc.Driver
- db.url=jdbc:mysql://localhost:3306/20231106_ssm?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
- db.username=root
- db.password=123456
- drop DATABASE if EXISTS 20231106_ssm;
- create DATABASE 20231106_ssm;
- use 20231106_ssm;
-
- CREATE TABLE `user` (
- `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
- `username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名',
- `password` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '密码',
- `age` int NULL DEFAULT NULL COMMENT '年龄',
- PRIMARY KEY (`id`) USING BTREE
- ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-
- INSERT INTO `user` VALUES (1, 'zhangsan', 'zhangsan123', 23);
- INSERT INTO `user` VALUES (2, 'lisi', 'lisi123', 24);
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
- <context:property-placeholder location="classpath:db.properties"/>
-
- <context:component-scan base-package="org.star" use-default-filters="true">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
-
- <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
- <property name="driverClassName" value="${db.driver}"/>
- <property name="url" value="${db.url}"/>
- <property name="username" value="${db.username}"/>
- <property name="password" value="${db.password}"/>
- bean>
-
- <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="typeAliasesPackage" value="org.star.entity.model"/>
- <property name="configuration">
- <bean class="org.apache.ibatis.session.Configuration">
- <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl">property>
- <property name="cacheEnabled" value="true">property>
- bean>
- property>
- <property name="mapperLocations" value="classpath:mappers/*.xml"/>
-
- bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
- <property name="basePackage" value="org.star.mapper"/>
- bean>
-
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">property>
- bean>
-
-
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
-
- <tx:attributes>
-
- <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
- tx:attributes>
- tx:advice>
-
- <aop:config>
- <aop:pointcut id="txPointCut" expression="execution(* org.star.service.UserService.*(..))"/>
- <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut">aop:advisor>
- aop:config>
-
- beans>
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
- <context:component-scan base-package="org.star" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
- <mvc:annotation-driven/>
-
- beans>
- "1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:applicationContext.xmlparam-value>
- context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
- <servlet>
- <servlet-name>springmvcservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:dispatcherServlet.xmlparam-value>
- init-param>
- servlet>
- <servlet-mapping>
- <servlet-name>springmvcservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
-
- <filter>
- <filter-name>encodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- <init-param>
- <param-name>forceRequestEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>encodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
- web-app>
- /**
- * @Author : 一叶浮萍归大海
- * @Date: 2023/11/6 11:34
- * @Description:
- */
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Accessors(chain = true)
- @ToString(callSuper = true)
- public class UserDO implements Serializable {
- /**
- * 编号
- */
- private Integer id;
-
- /**
- * 用户名
- */
- private String username;
-
- /**
- * 密码
- */
- private String password;
-
- /**
- * 年龄
- */
- private Integer age;
- }
- /**
- * @Author : 一叶浮萍归大海
- * @Date: 2023/11/6 11:35
- * @Description:
- */
- public interface UserMapper {
-
- /**
- * 添加用户
- * @param param
- */
- void saveUser(UserDO param);
-
- /**
- * 查询所有的用户
- * @return
- */
- List
listAllUser(); -
- /**
- * 根据id查询用户
- * @param id
- * @return
- */
- UserDO getUserById(Integer id);
- }
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="org.star.mapper.UserMapper">
-
- <insert id="saveUser">
- insert into `user`(username,password,age) values (#{username},#{password},#{age})
- insert>
-
-
- <select id="listAllUser" resultType="userDO">
- select * from `user`
- select>
-
- <select id="getUserById" resultType="userDO">
- select * from `user` where id = #{id}
- select>
- mapper>
- /**
- * @Author : 一叶浮萍归大海
- * @Date: 2023/11/6 11:36
- * @Description:
- */
- @Service
- public class UserService {
-
- @Autowired
- private UserMapper userMapper;
-
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
- public List
listAllUser() { -
- return userMapper.listAllUser();
- }
-
- public void saveUser(UserDO param) {
- userMapper.saveUser(param);
- }
-
- /**
- * 不加此注解的话,将会执行多次数据库查询
- * 参考:https://blog.51cto.com/u_15942107/6019692
- * @param id
- * @return
- */
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
- public UserDO getUserById(Integer id) {
- UserDO u1 = userMapper.getUserById(id);
- UserDO u2 = userMapper.getUserById(id);
- UserDO u3 = userMapper.getUserById(id);
- System.out.println(u1);
- System.out.println(u2);
- System.out.println(u3);
-
- return userMapper.getUserById(id);
- }
- }
- @Slf4j
- @RestController
- public class UserController {
-
- @Autowired
- private UserService userService;
-
- @PostMapping("/saveUser")
- public String saveUser(@RequestBody UserDO param) {
- log.info("UserController saveUser param:{}", param);
- userService.saveUser(param);
- return "SUCCESS";
- }
-
- @GetMapping("/listAllUser")
- public List
listAllUser() { - log.info("=================>listAllUser");
- return userService.listAllUser();
- }
-
- @GetMapping("/getUserById/{id}")
- public UserDO getUserById(@PathVariable("id") Integer id) {
- log.info("=================>getUserById id:{}",id);
- return userService.getUserById(id);
- }
-
- }
http://localhost:8080/listAllUser


