jdbctemplate

JdbcTemplate增删改查案例

邮差的信 提交于 2019-12-03 07:09:45
JdbcTemplate增删改查案例 架包 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> 实体类 public class Student implements Serializable { private Integer stuId; private String stuName; private String stuAddress; public Integer getStuId() { return stuId; }

Spring-JDBCTemplate

你离开我真会死。 提交于 2019-12-03 07:00:59
Spring 框架针对数据库开发中的应用提供了 JDBCTemplate 类,该类是 Spring 对 JDBC 支持的核心,它提供了所有对数据库操作功能的支持。 Spring 框架提供的JDBC支持主要由四个包组成,分别是 core(核心包)、object(对象包)、dataSource(数据源包)和 support(支持包),org.springframework.jdbc.core.JdbcTemplate 类就包含在核心包中。作为 Spring JDBC 的核心,JdbcTemplate 类中包含了所有数据库操作的基本方法。 JdbcTemplate 类继承自抽象类 JdbcAccessor,同时实现了 JdbcOperations 接口。其直接父类 JdbcAccessor 为子类提供了一些访问数据库时使用的公共属性,具体介绍如下。 1)DataSource 其主要功能是获取数据库连接,具体实现时还可以引入对数据库连接的缓冲池和分布式事务的支持,它可以作为访问数据库资源的标准接口。 2)SQLExceptionTranslator org.springframework.jdbc.support.SQLExceptionTranslator 接口负责对 SQLException 进行转译工作。通过必要的设置或者获取 SQLExceptionTranslator 中的方法

JdbcTemplate实现增删改查操作

时光总嘲笑我的痴心妄想 提交于 2019-12-03 06:51:30
JdbcTemplate 介绍 为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架,Spring Boot Spring Data-JPA。 作为 SpringJDBC 框架的核心, JDBC 模板的设计目的是为不同类型的JDBC操作提供模板方法. 每个模板方法都能控制整个过程,并允许覆盖过程中的特定任务。 通过这种方式,可以在尽可能保留灵活性的情况下,将数据库存取的工作量降到最低。 JdbcTemplate 方法介绍 JdbcTemplate主要提供以下五类方法: 1、execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句; Execute、executeQuery、executeUpdate 2、update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句 SQL SERVCER(GO SQL语句 GO) ; 3、query方法及queryForXXX方法:用于执行查询相关语句; 4、call方法:用于执行存储过程、函数相关语句。 JdbcTemplate 实现增删改查 JdbcTemplate添加数据 1. 使用配置实现 1.1 创建实体类 public class Account { private

JdbcTemplate增删改

爱⌒轻易说出口 提交于 2019-12-03 06:50:13
( 1 ) Accountsdao 层 //删除单个账户 int delaccount(Integer accountid); //添加单个用户 int addaccount(Accounts accounts); //修改单个用户 int updateaccount(Accounts accounts); 代码实现 (2) AccountdaoImpl 实现类 @Repository public class AccountDaoImpl implements AccountsDao { @Resource private JdbcTemplate jdbcTemplate; @Override public int delaccount(Integer accountid) { String sql="delete from accounts where accountid=5"; int count = jdbcTemplate.update(sql); return count; } @Override public int addaccount(Accounts accounts) { String sql="INSERT INTO accounts(accountname,balance) VALUES (?,?)"; int add = jdbcTemplate

JdbcTemplate经典案例

坚强是说给别人听的谎言 提交于 2019-12-03 06:48:34
一、JdbcTemplate 案例配置式 ( 1 )导入依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> View Code (2) jdbc.properties 连接数据库 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/account?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=123 View Code (3) Accounts 实体类 package cn.spring.accountstest.entity; public class Accounts { private Integer accountid;

JdbcTemplate query close database connection

我们两清 提交于 2019-12-03 06:42:47
I use jpa with hibernate. I have following method: @Transactional public void myMethod(){ ... firstJDBCTemplateQuery(); secondJDBCTemplateQuery(); ... } firstJDBCTemplateQuery works, but it closes connection to database. When second secondJDBCTempolateQuery is executed java.sql.SQLException: Connection is closed exception is thrown what causes org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction ... My configuration: EDIT <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc

Return a list, I already have a rowmapper implementation

柔情痞子 提交于 2019-12-03 05:32:20
In my UserDao I want to return a list of users. I already have a UserRowMapper that implements RowMapper<User> . How can I do this? I tried: List rows = getJdbcTemplate().queryforList("select * from users"); for(Map row : rows) { } But wasn't sure how to use my UserRowMapper to populate a User object and insert it into my list of users List. BTW, is this the best generic list I shoudl be using: List<User> users = new ArrayList<User>(); ? The Awnry Bear Use JdbcTemplate.query(String sql, RowMapper<T> rowMapper, Object... args) , and you can pass in a variable number of values for the ?

Spring JDBC入门

假装没事ソ 提交于 2019-12-03 05:28:31
1. Spring JDBC 模板 Spring 是EE的一站式开发框架,对持久层同样提供了支持:ORM模块和 JDBC模板 Spring 提供了很多模板简化了开发 spring中提供了一个可以操作数据库的对象,对象封装了jdbc技术。 与DBUtils中QueryRunner非常相似 2. JDBC模板的使用 基本使用方法 创建数据库 mysql> show create table account; | Table | Create Table| account | CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(10) DEFAULT NULL, `money` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 public class JDBCTemplateTest { @Test public void testadd(){ // 创建连接池 DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc

JDBC 操作

懵懂的女人 提交于 2019-12-03 05:22:24
简单的 JDBC 操作主要有: JdbcTemplate query queryForObject queryForList update execute 简单使用如下所示。 初始化数据库 springboot 会自动执行 resources 文件夹下的 data.sql 和 schema.sql。 schema.sql CREATE TABLE FOO (ID INT IDENTITY, BAR VARCHAR(64)); data.sql INSERT INTO FOO (ID, BAR) VALUES (1, 'a'); INSERT INTO FOO (ID, BAR) VALUES (2, 'b'); 插入数据 @Autowired private JdbcTemplate jdbcTemplate; @Autowired private SimpleJdbcInsert simpleJdbcInsert; public void insertData() { Arrays.asList("a", "b").forEach(bar -> { jdbcTemplate.update("INSERT INTO FOO (BAR) VALUES (?)", bar); }); HashMap<String, String> row = new HashMap<>(); row

Spring Boot: Jdbc javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify

时光毁灭记忆、已成空白 提交于 2019-12-03 05:05:16
I am currently learning more about implementing JDBC and using databases in a Spring Boot webapp, and I encountered the following Stack Trace written in the bottom of the post. I have created a simple Employee model, and I am trying to execute some database code on the same class which my main() lies in. The model and the main class are the only two java files existing in this whole project. I am trying to implement the following run() code that overrides the one from the interface, CommandLineRunner, but I do not get the logs that should come after log.info("Part A:") : log.info("Part A:")