Spring中JDBCTemplate的入门
Spring是IOC和AOP的容器框架,一站式的框架
连接数据库的步骤:[必须会写]
Spring当中如何配置连接数据库?
第一步配置核心配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 启动Spring注解 --> <context:annotation-config/> <!-- 扫描,如果有多个使用逗号分隔 --> <context:component-scan base-package="com.shxt"/> <!-- 加载属性文件,classpath和classpath*的区别 --> <context:property-placeholder location="classpath:/jdbc.properties"/> <!-- 配置数据源信息 --> <bean id="shxtDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置完成 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="shxtDataSource"/> </bean> </beans>
package com.shxt.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; public class JDBCTest { private ApplicationContext ac =null; private JdbcTemplate jdbcTemplate = null; @Before public void init(){ //读取核心配置文件 ac = new ClassPathXmlApplicationContext("beans.xml"); //获取JdbcTemplate对象,通过id的方式, JdbcTemplate.class就是强转 jdbcTemplate = ac.getBean("jdbcTemplate",JdbcTemplate.class);//强转 } @Test public void 添加角色的操作_第一种方式(){ String sql = "insert into sys_role(role_name,role_desc) values ('悟空','齐天大圣')"; //不正规 //默认情况下JDBC的事务是自动提交的,而大部分的持久层框架是需要手动提交的 int rownum = jdbcTemplate.update(sql);//delete/update/insert的sql语句 System.out.println(rownum); } }
代码优化
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 启动Spring注解 --> <context:annotation-config/> <!-- 扫描,如果有多个使用逗号分隔 --> <context:component-scan base-package="com.shxt"/> <!-- 加载属性文件,classpath和classpath*的区别 --> <context:property-placeholder location="classpath:/jdbc.properties"/> <!-- 配置数据源信息 --> <bean id="shxtDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="shxtDataSource" /> </beans>
代码说明: <bean id="shxtDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName[对应的是set方法]="${jdbc.driver}"
p:url[对应的是set方法]="${jdbc.url}" p:username[对应的是set方法]="${jdbc.username}"
p:password[对应的是set方法]="${jdbc.password}" /> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource[对应的是set方法]-ref[引用]="shxtDataSource" />
@Test public void 添加角色的操作_第二种方式_推荐方式_预处理方式(){ String sql = "insert into sys_role(role_name,role_desc) values (?,?)"; //int rownum = jdbcTemplate.update(sql, new Object[]{"八戒","天蓬元帅"}); int rownum = jdbcTemplate.update(sql, "八戒","天蓬元帅"); System.out.println(rownum); }
如果使用Hibernate或者Mybatis类似的持久层框架,他们都可以通过配置返回你添加数据的主键
Mybatis是如何配置?请补充代码,映射文件内容
---->>>> 如果涉及到Oracle数据库,建议使用官方写法,因为Oracle没有自增长的字段,是通过序列完成自增长操作
final String INSERT_SQL = "insert into my_test (name) values(?)"; // 老版本因为是内部类,所有需要使用final
final String name = "Rob"; KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] {"id"});//id为主键字段
ps.setString(1, name);//PreparedStatement 1代表第一个问好
return ps;
}
},
keyHolder); // keyHolder.getKey() now contains the generated key
@Test public void 添加角色的操作_关于主键_前提必须使用自增长的方式(){ String sql = "insert into sys_role(role_name,role_desc) values (?,?)"; Role role = new Role(); role.setRole_name("唐僧"); role.setRole_desc("金蝉子"); KeyHolder keyHolder = new GeneratedKeyHolder();//使用Ctrl+T的方式,看KeyHolder的实现类 jdbcTemplate.update(new PreparedStatementCreator() { @Override //一期内容 public PreparedStatement createPreparedStatement(Connection con) throws SQLException { //1.获取预处理对象 PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, role.getRole_name());//注意 1代表的是第一个问号 ps.setString(2, role.getRole_desc());//注意 2代表的是第二个问号 return ps; } }, keyHolder); int pk = keyHolder.getKey().intValue();//getKey()返回为Number类型 role.setRole_id(pk); System.out.println(role.getRole_id()); }
关于更新操作和删除操作类似,如果涉及到批量添加和更新问题,请自行扩展学习
测试查询操作:
@Test public void 通过查询返回单一的值(){ String sql = "select count(*) from sys_role where role_name like ?"; Integer count = jdbcTemplate.queryForObject(sql, Integer.class, "%1%");//Integer.class 强转 //Integer count = jdbcTemplate.queryForObject(sql, new Object[]{"%员%"}, Integer.class); System.out.println(count); }
返回一条数据的形式:
@Test public void 返回一条数据_Map形式(){ String sql = "select * from sys_role where role_id=?"; Map<String,Object> map = jdbcTemplate.queryForMap(sql, 405); System.out.println(map); }
@Test public void 返回一条数据_对象形式(){ String sql = "select * from sys_role where role_id=?";//一期关系 Role role = jdbcTemplate.queryForObject(sql, new Object[]{405},new RowMapper<Role>(){ @Override //Java的内部类?匿名类? public Role mapRow(ResultSet rs, int rowNum) throws SQLException { Role role = new Role(); role.setRole_id(rs.getInt("role_id")); role.setRole_name(rs.getString("role_name")); role.setRole_desc(rs.getString("role_desc")); role.setRole_photo(rs.getString("role_photo")); role.setRole_status(rs.getString("role_status")); return role; } }); System.out.println(role); }
返回列表数据
@Test public void 返回列表数据_List_Map形式(){ String sql = "select * from sys_role where role_name like ?"; List<Map<String,Object>> dataList = jdbcTemplate.queryForList(sql, "%1%"); System.out.println(dataList); }
@Test public void 返回列表数据_List_对象形式(){ String sql = "select * from sys_role where role_name like ?"; List<Role> roleList = jdbcTemplate.query(sql, new Object[]{"%员%"}, new RowMapper<Role>(){ @Override public Role mapRow(ResultSet rs, int rowNum) throws SQLException { Role role = new Role(); role.setRole_id(rs.getInt("role_id")); role.setRole_name(rs.getString("role_name")); role.setRole_desc(rs.getString("role_desc")); role.setRole_photo(rs.getString("role_photo")); role.setRole_status(rs.getString("role_status")); return role; } }); System.out.println(roleList); }
我们发现 返回列表数据_List_对象形式()和返回一条数据_对象形式() 中的代码有重复性质!
public class RoleRowMapper implements RowMapper<Role> { @Override public Role mapRow(ResultSet rs, int rowNum) throws SQLException { Role role = new Role(); role.setRole_id(rs.getInt("role_id")); role.setRole_name(rs.getString("role_name")); role.setRole_desc(rs.getString("role_desc")); role.setRole_photo(rs.getString("role_photo")); role.setRole_status(rs.getString("role_status")); return role; } }
@Test public void 返回一条数据_对象形式_优化(){ String sql = "select * from sys_role where role_id=?";//一期关系 Role role = jdbcTemplate.queryForObject(sql, new Object[]{405},new RoleRowMapper()); //实现类 System.out.println(role); }
@Test public void 返回列表数据_List_对象形式_优化(){ String sql = "select * from sys_role where role_name like ?"; List<Role> roleList = jdbcTemplate.query(sql, new Object[]{"%员%"}, new RoleRowMapper()); //实现类 System.out.println(roleList); }
个人推荐: 超精简版[自己命名的,私人珍藏],通过领域模型自动完成映射
@Test //你需要保持数据库的字段名和属性名保持一致,[没有说完全一致] public void 返回一条数据_对象形式_超精简版(){ String sql = "select * from sys_role where role_id=?";//一期关系 Role role = jdbcTemplate.queryForObject(sql, new Object[]{405},new BeanPropertyRowMapper<Role>(Role.class)); System.out.println(role); } @Test public void 返回列表数据_List_对象形式_超精简版(){ String sql = "select * from sys_role where role_name like ?"; List<Role> roleList = jdbcTemplate.query(sql, new Object[]{"%员%"}, new BeanPropertyRowMapper<Role>(Role.class)); System.out.println(roleList); } @Test public void 返回列表数据_List_对象形式_超精简版_一致的解释(){ String sql = "select role_name , role_desc role_photo1 from sys_role"; List<Role> roleList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Role>(Role.class)); System.out.println(roleList); }
模拟权限分配操作
@Test public void 权限分配() throws Exception{ //1.客户端复选框传递过来一个数组 1,2 菜单的ID Integer[] menus = new Integer[]{1,2}; //2.声明数组 String[] sqls = new String[menus.length+1];//0,1,2 //3.通过ROle_Id 200 删除中间表的信息 sqls[0] = "delete from role_link_menu where fk_role_id=200";// //3.新数据添加到中间表 for (int i=0;i<menus.length;i++) { //1,2 sqls[i+1] = "insert into role_link_menu (id,fk_role_id,fk_menu_id) values ('"+UUID.randomUUID().toString()+"',200,"+menus[i]+")"; } jdbcTemplate.batchUpdate(sqls); //MyBatis的实现方式 }
-----------------------------------------
********等价操作*********
-----------------------------------------
通过MyBatis的实现方式,自学内容,MySQL下的操作,设置如下!
&allowMultiQueries=true 新增内容如下:
shxt.driver=com.mysql.jdbc.Driver shxt.url=jdbc:mysql://127.0.0.1:3308/xy37_rbac??useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true shxt.username=root shxt.password=shxt
接口中定义方法,如下推荐使用Map public void permission(Map<String,Object> map);
映射文件代码如下: <delete id="permission" parameterType="map" statementType="PREPARED"> DELETE FROM role_link_menu WHERE fk_role_id=#{role_id}; <foreach collection="menus" item="menu_id" > INSERT INTO role_link_menu (id, fk_role_id, fk_menu_id) VALUES ((SELECT UUID()),#{role_id},#{menu_id}); </foreach> </delete>
测试代码: @Test public void 多条SQL语句执行(){ SqlSession sqlSession = null; try { sqlSession = MyBatisUtils.getSqlSession(); Map<String, Object> map = new HashMap<String,Object>(); map.put("role_id", 200); map.put("menus", new Object[]{1,2,3,6}); sqlSession.getMapper(UserMapper.class).permission(map); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); }finally { MyBatisUtils.closeSqlSession(sqlSession); } }
Spring中JDBCTemplate的入门的更多相关文章
- Spring中IoC的入门实例
Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...
- Spring 中jdbcTemplate 实现执行多条sql语句
说一下Spring框架中使用jdbcTemplate实现多条sql语句的执行: 很多情况下我们需要处理一件事情的时候需要对多个表执行多个sql语句,比如淘宝下单时,我们确认付款时要对自己银行账户的表里 ...
- Spring中JdbcTemplate的基础用法
Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...
- 【sping揭秘】19、关于spring中jdbctemplate中的DataSource怎么来呢
我们这是可以正好借助之前学的factorybean类,自己吧jdbctemplate加载到spring容器中,我们可以封装多个这种对象,那么可以实现针对不同的数据库的jdbctemplate 首先我们 ...
- 2018.12.25 Spring中JDBCTemplate模版API学习
1 Spring整合JDBC模版 1.1 spring中土拱了一个可以操作数据库的对象.对象封装了jdbc技术 JDBCTemplateJDBC模板对象 1.2 与DBUtils中的QueryRunn ...
- SSM-Spring-19:Spring中JdbcTemplate
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- Spring自带一个ORM持久化框架JdbcTemplate,他可以说是jdbc的加强版,但是对最细微的控制肯 ...
- Spring中JdbcTemplate中使用RowMapper
转自:https://blog.csdn.net/u012661010/article/details/70049633 1 sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类 ...
- Spring中jdbcTemplate的用法实例
一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象. 第一种方式:我们可以在自己定 ...
- spring中JdbcTemplate使用
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
随机推荐
- Java中二进制字节与十六进制互转
在Java中字节与十六进制的相互转换主要思想有两点: 1.二进制字节转十六进制时,将字节高位与0xF0做"&"操作,然后再左移4位,得到字节高位的十六进制A;将字节低位与0 ...
- C#读写config配置文件--读取配置文件类
一:通过Key访问Value的方法: //判断App.config配置文件中是否有Key(非null) if (ConfigurationManager.AppSettings.HasKeys()) ...
- centos 5的yum源无法使用的解决方法( 转载)
由于centos 5 已经停更.于是导致yum源也不能用了. 例如安装screen的时候提示 Determining fastest mirrors* base: denver.gaminghost. ...
- js笔记3
1字符串 replace("","")替换,前面为要替换什么,后面为替换的内容只能替换一个 2DOM 时间三要素 事件源 谁身上发生的行为 事件 单机 双击 事 ...
- 【Django】视图系统
目录 FBV 与 CBV 给视图加装饰器 使用装饰器装饰CBV 关于dispatch()方法 Request对象 与 Response对象 request对象 response对象 @ *** 一个视 ...
- 通用查询实现方案(可用于DDD)[附源码] -- 设计思路
原文:通用查询实现方案(可用于DDD)[附源码] -- 设计思路 [声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3988592.html). ...
- 【Henu ACM Round #13 A】 Hulk
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. [代码] #include <bits/stdc++.h> using namespace std; int m ...
- Json学习总结(1)——Java和JavaScript中使用Json方法大全
摘要:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语 ...
- java设计模式--事件监听器模式和观察者模式
监听器模式:事件源经过事件的封装传给监听器,当事件源触发事件后,监听器接收到事件对象可以回调事件的方法 观察者模式:观察者(Observer)相当于事件监听者,被观察者(Observable)相当于事 ...
- [Python] Working with file
Python allows you to open a file, do operations on it, and automatically close it afterwards using w ...