1.项目结构
2.pom.xml和application.properties
pom.xml
<?xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!-- devtools --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- 数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.21</version></dependency><!--jdbc --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.properties
#jdbc连接
spring.datasource.url&#61;jdbc:mysql://localhost:3306/springboottest?useUnicode&#61;true&characterEncoding&#61;utf8&serverTimezone&#61;UTC
spring.datasource.username&#61;root
spring.datasource.password&#61;123456
spring.datasource.driver-class-name&#61;com.mysql.cj.jdbc.Driver
#数据源
spring.datasource.type&#61;com.alibaba.druid.pool.DruidDataSource
#tomcat端口
server.port&#61;8888
3.UserController
package com.example.demo.controller;import java.util.HashMap;
import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import com.example.demo.bean.User;
import com.example.demo.service.UserService;&#64;Controller
&#64;RequestMapping("user")
public class UserController {&#64;Autowiredprivate UserService userService;&#64;ResponseBody&#64;RequestMapping("login")public Map<String,String> login(User user){HashMap<String, String> map &#61; new HashMap<String,String>();try {User user2 &#61; userService.findUser(user);if(null !&#61;user2) {map.put("success", user2.getName()&#43;":欢迎您登录成功");}else {map.put("success", "用户名和密码不正确");}} catch (Exception e) {e.printStackTrace();map.put("error", "程序出现异常");}return map;}
}
4.UserService
package com.example.demo.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;&#64;Service
public class UserService {&#64;Autowiredprivate UserDao userDao;public User findUser(User user) {User user2 &#61; userDao.findUser(user);return user2;}}
5.UserDao
package com.example.demo.dao;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;import com.example.demo.bean.User;&#64;Repository
public class UserDao {&#64;Autowiredprivate JdbcTemplate jdbcTemplate;public User findUser(User user) {String sql&#61;"select * from user where name&#61;? and pass&#61;?";RowMapper<User> mapper &#61; BeanPropertyRowMapper.newInstance(User.class);ArrayList<Object> agrList &#61; new ArrayList<Object>();agrList.add(user.getName());agrList.add(user.getPass());List<User> userList &#61; jdbcTemplate.query(sql, mapper,agrList.toArray());return userList.get(0);}}
6.spring boot添加事务
在service的类上添加&#64;Transactional&#xff0c;表示这个类所有方法都添加事务。
在service的单个方法上添加&#64;Transactional&#xff0c;表明这个方法添加事务。
&#64;Service
&#64;Transactional
public class UserService {&#64;Autowiredprivate UserDao userDao;public User findUser(User user) {User user2 &#61; userDao.findUser(user);return user2;}&#64;Transactionalpublic void addUser(User user) {userDao.addUser(user);System.out.println(1/0);userDao.addUser(user);}}