Springboot集成JDBC
1,pom.xml配置jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2,配置数据源信息
server:
port: 8088
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ssm_db?serverTimezone=Asia/Shanghai
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5 #初始化时建立物理连接的个数
minIdle: 1 #最小连接池数量
maxActive: 20 #最大连接池数量
LoginDao.java
package com.ffyc.news.dao;
import com.ffyc.news.model.Admin;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public class LoginDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Transactional
public void intsert(){
jdbcTemplate.update("insert into admin(account,pwd) value (?,?)","王五","5555");
System.out.println(10/0);
jdbcTemplate.update("insert into admin(account,pwd) value (?,?)","ikun","666");
}
}
LoginService.java
package com.ffyc.news.service;
import com.ffyc.news.dao.LoginDao;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LoginService {
@Autowired
LoginDao loginDao;
public void test(){
loginDao.intsert();
}
}
LoginController.java
package com.ffyc.news.web;
import com.ffyc.news.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/admin/login/")
public class LoginController {
@Autowired
LoginService loginService;
@RequestMapping("/login/")
public void Login(){
loginService.test();
System.out.println("success");
}
}