springboot使用jdbcTemplate连接数据库
springboot使用jdbcTemplate连接数据库
1.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>spring-data_JpaAndJdbcTemplate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-mybatis</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2.application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.Student:
package com.example.springmybatis.entity; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class Student { @Id
@GeneratedValue
private String name;
@Column(nullable = true)
private int age;
@Column(nullable = true)
private String address; private String collage; private String sex; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getCollage() {
return collage;
} public void setCollage(String collage) {
this.collage = collage;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", collage='" + collage + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
4.UserService:
package com.example.springmybatis.service_jdbc; import com.example.springmybatis.entity.Student; public interface UserService {
void create(String name, Integer age);
void deleteByName(String name);
Integer getAllUsers();
void deleteAllUsers();
Student select(String name, int age);
}
5.UserServiceImpl:
package com.example.springmybatis.service_jdbc; import com.example.springmybatis.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service; import java.sql.ResultSet;
import java.sql.SQLException; @Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void create(String name, Integer age) {
jdbcTemplate.update("insert into student(NAME, AGE) values(?, ?)", name, age);
}
@Override
public void deleteByName(String name) {
jdbcTemplate.update("delete from student where NAME = ?", name);
}
@Override
public Integer getAllUsers() {
return jdbcTemplate.queryForObject("select count(1) from student", Integer.class);
}
@Override
public void deleteAllUsers() {
jdbcTemplate.update("delete from student");
}
@Override
public Student select(String name, int age) {
return jdbcTemplate.queryForObject("select name, age, address, collage, sex from student where NAME = ? and age = ?", new MyRowMapper(), name, age);
} public class MyRowMapper implements RowMapper<Student> { @Override
public Student mapRow(ResultSet resultSet, int i) throws SQLException {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
String collage = resultSet.getString("collage");
String sex = resultSet.getString("sex");
Student student = new Student();
student.setName(name);
student.setAddress(address);
student.setAge(age);
student.setCollage(collage);
student.setSex(sex); return student;
}
}
}
6.UserController:
package com.example.springmybatis.Controller; import com.example.springmybatis.entity.Student;
import com.example.springmybatis.service_jdbc.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
public class UserController { @Autowired
private UserService userService; /**
* JdbcTemplate连接方式
* @param name
* @param age
* @return
* @throws Exception
*/
@RequestMapping(value = "/jdbc", method = RequestMethod.GET)
@ResponseBody
public Student GetNameJdbc(@RequestParam(value = "name", defaultValue = "易中天") String name
, @RequestParam(value = "age", defaultValue = "55") int age)throws Exception{ Student user = userService.select(name, age);
return user;
}
}
7.spring boot启动项:
package com.example.springmybatis; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringDataJpaAPP { public static void main(String[] args) {
SpringApplication.run(SpringDataJpaAPP.class, args);
}
}
8.sql:
CREATE TABLE `student` (
`name` varchar(100) NOT NULL,
`age` int(10) NOT NULL,
`address` varchar(100) NOT NULL,
`collage` varchar(100) NOT NULL,
`sex` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
springboot使用jdbcTemplate连接数据库的更多相关文章
- springboot之JdbcTemplate
springboot可以使用JdbcTemplate进行数据库访问,代码如下 添加pom文件 <parent> <groupId>org.springframework.boo ...
- SpringBoot使用JdbcTemplate
前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...
- springboot 整合jdbcTemplate
springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: ...
- springboot使用Jpa连接数据库
springboot使用Jpa连接数据库 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)
Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...
- Springboot 使用JdbcTemplate
Springboot 使用JdbcTemplate book package com.draymonder.book.jdbc; public class Book { private Integer ...
- 关于SpringBoot集成JDBCTemplate的RowMapper问题
JdbcTemplate 是Spring提供的一套JDBC模板框架,利用AOP 技术来解决直接使用JDBC时大量重复代码的问题.JdbcTemplate虽然没有MyBatis 那么灵活,但是直接使用J ...
- SpringBoot整合JdbcTemplate连接Mysql
import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProper ...
- springboot集成jdbcTemplate
这里使用springboot自带的jdbcTemplate连接mysql数据库 1. 添加依赖包 <!-- jdbc --> <dependency> <groupId& ...
随机推荐
- 使用python画2D线条
"""用于验证整体趋势正确性,不做关闭操作时保持显示""" #!python3 #-*- coding:utf-8 -*- import m ...
- Mac安装Homebrew的那些事儿
Mac安装Homebrew的那些事儿 最近小明刚换置了一个 Mac 本,想搭建一个属于自己的博客网站,需要用到 Node.js 环境,而Node.js 在 MacOS 中是由 Homebrew 进行安 ...
- Java——检测其他线程的状态以及启动已死亡的线程
这次这个的思路是在主类中维护一个map,map的key是线程名,value是线程的状态,然后创建周期执行的线程通过检测这个map来判断进程的状态,如果有死亡的进程就把该进程启动. 首先是主类,这里的m ...
- exported function xxx should have comment or be unexported
0x00 问题 exported function xxx should have comment or be unexported. 0x01 解决 https://golang.org/s/sty ...
- Spring boot实战项目整合阿里云RocketMQ (非开源版)消息队列实现发送普通消息,延时消息 --附代码
一.为什么选择RocketMQ消息队列? 首先RocketMQ是阿里巴巴自研出来的,也已开源.其性能和稳定性从双11就能看出来,借用阿里的一句官方介绍:历年双 11 购物狂欢节零点千万级 TPS.万亿 ...
- 使用PowerShell 测试DNS
运行环境:Windows Server 2012 R2 获取服务器DNS命令,下面的仅获取一个dns (nslookup sql.ciras.com)[1].split(':')[1].trim() ...
- Python机器学习之数据探索可视化库yellowbrick-tutorial
背景介绍 从学sklearn时,除了算法的坎要过,还得学习matplotlib可视化,对我的实践应用而言,可视化更重要一些,然而matplotlib的易用性和美观性确实不敢恭维.陆续使用过plotly ...
- 在github上搭建个人博客并在线更新
换博客比更博还勤的我终于决定写一篇博客搭建教程了.. FAQ Q:\(hexo\)需要本地编译.\(jekyll\)虽然可以直接上传\(md\)..但是如果在github上直接编译也太难受了叭,毕竟不 ...
- ASP.NET Core on K8S深入学习(7)Dashboard知多少
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 在第二篇<部署过程解析与Dashboard>中介绍了如何部署Das ...
- jvm系列(六):Java服务GC参数调优案例
本文介绍了一次生产环境的JVM GC相关参数的调优过程,通过参数的调整避免了GC卡顿对JAVA服务成功率的影响. 这段时间在整理jvm系列的文章,无意中发现本文,作者思路清晰通过步步分析最终解决问题. ...