1.项目结构
2.pom.xml和application.properties
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 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=jdbc:mysql://localhost:3306/springboottest?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#tomcat端口
server.port=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;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("login")
public Map<String,String> login(User user){
HashMap<String, String> map = new HashMap<String,String>();
try {
User user2 = userService.findUser(user);
if(null !=user2) {
map.put("success", user2.getName()+":欢迎您登录成功");
}else {
map.put("success", "用户名和密码不正确");
}
} catch (Exception e) {
// TODO Auto-generated catch block
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;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public User findUser(User user) {
User user2 = 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;
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public User findUser(User user) {
String sql="select * from user where name=? and pass=?";
RowMapper<User> mapper = BeanPropertyRowMapper.newInstance(User.class);
ArrayList<Object> agrList = new ArrayList<Object>();
agrList.add(user.getName());
agrList.add(user.getPass());
List<User> userList = jdbcTemplate.query(sql, mapper,agrList.toArray());
return userList.get(0);
}
}
6.spring boot添加事务
在service的类上添加@Transactional,表示这个类所有方法都添加事务。
在service的单个方法上添加@Transactional,表明这个方法添加事务。
@Service
@Transactional
public class UserService {
@Autowired
private UserDao userDao;
public User findUser(User user) {
User user2 = userDao.findUser(user);
return user2;
}
@Transactional
public void addUser(User user) {
userDao.addUser(user);
System.out.println(1/0);
userDao.addUser(user);
}
}