标签:innodb cto bean scope view mic cli tle ota
1.依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2.配置属性
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=ivy spring.datasource.password=*******
3.数据表结构
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `age` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8
4.项目结构
5.源码
(1).Domain
package com.example.demo.jdbc_tx.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private Integer id; private String name; private Integer age; public User(String name, int age) { this.name = name; this.age = age; } }
(2).Mapper
package com.example.demo.jdbc_tx.mapper; import com.example.demo.jdbc_tx.domain.User; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; public class UserMapper implements RowMapper { @Override public Object mapRow(ResultSet resultSet, int i) throws SQLException { return new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getInt("age")); } }
(3).Service接口
package com.example.demo.jdbc_tx.mapper; import com.example.demo.jdbc_tx.domain.User; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; public class UserMapper implements RowMapper { @Override public Object mapRow(ResultSet resultSet, int i) throws SQLException { return new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getInt("age")); } }
(4).Service实现
package com.example.demo.jdbc_tx.service.impl; import com.example.demo.jdbc_tx.domain.User; import com.example.demo.jdbc_tx.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; @Service public class UserServiceImp implements UserService { private final JdbcTemplate jdbcTemplate; @Autowired public UserServiceImp(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void save(User user) { jdbcTemplate.update("insert into user(name, age) values (?, ?)", user.getName(), user.getAge()); throw new RuntimeException("事务异常,回滚!"); } }
(5).Controller测试
package com.example.demo.jdbc_tx.controller; import com.example.demo.jdbc_tx.domain.User; import com.example.demo.jdbc_tx.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("user") public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @GetMapping("save") public void save(User user) { userService.save(user); } }
标签:innodb cto bean scope view mic cli tle ota
原文地址:https://www.cnblogs.com/itivy/p/11706251.html