spring+jdbc模板
准备工作
准备工具类和文件
1、*.properties文件
2、PropertiesUtils.javadriver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ums userName=root password=123456
package utils; /** * * @author 兴华 * @申明:对实体字符串封装 */ import java.util.Properties; public class PropertiesUtils { private static Properties properties = null; static { // 创建对象 properties = new Properties(); try { // 加载文件流 properties.load(PropertiesUtils.class.getResourceAsStream("/blog.properties")); } catch (Exception e) { e.printStackTrace(); } } /** * 根据键获取值 * @param key * @return */ public static String get(String key) { return properties.getProperty(key); } }
jdbcTemplate增删改查
装不完善,与JDBC有些相似。但是spring自带的数据库操作简化了jdbc操作。
持久化操作
持久化操作,均可以调用jdbcTemplate的update方法实现。
@Test public void testJDBCTemplateUpdate(){ //配置数据源信息 DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/ums"); dataSource.setUsername("root"); dataSource.setPassword("root"); //创建JDBC模板,将数据源对象放进模板中 JdbcTemplate template = new JdbcTemplate(dataSource); String sql = "insert into `user`"+ "(id,user_name,`password`) values(?,?,?)"; String id = StringUtils.uuid(); String userName = StringUtils.getChineseName(); String password = MD5Util.GetMD5Code(userName); //利用模板对象的update方法进行持久化操作(增删改) int rows = template.update(sql, id,userName,password); System.err.println("受影响的行数:"+rows); }
修改数据:测试类
//spring+jdbc模板 @Test public void te2() { //配置jdbc DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/ums"); dataSource.setUsername("root"); dataSource.setPassword("123456"); //建立jdbc模板 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // String sql="insert into goods_classfy (id,name)values(?,?)"; // int rows = jdbcTemplate.update(sql,"4", "张三"); String sql = "update goods_classfy set name=? where id=? "; int rows = jdbcTemplate.update(sql, "张三", "4"); System.out.println("受影响行数-----》" + rows); }
/** * 获取多条数据 */ @Test public void myRowMapper(){ //配置jdbc DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/ums"); dataSource.setUsername("root"); dataSource.setPassword("123456"); //建立jdbc模板 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = " SELECT * FROM goods_classfy "; List<GoodsClassfy> goodsClassfyList = jdbcTemplate.query(sql, new MyRowMapper<GoodsClassfy>()); System.out.println(goodsClassfyList); }
创建一个MyRowMapper类:MyRowMapper.java创建GoodsClassfy实体类;GoodsClassfy.java
public class MyRowMapper<T> implements RowMapper<T> { @Override public T mapRow(ResultSet resultSet, int i) throws SQLException { T t=null; try { GoodsClassfy goodsClassfy=new GoodsClassfy(); goodsClassfy.setId(resultSet.getString("id")); goodsClassfy.setName((resultSet.getString("name"))); t=(T)goodsClassfy; }catch (Exception e){ System.out.println("没有数据了"); } return t; } }
package com.keduox.entity; import java.io.Serializable; public class GoodsClassfy implements Serializable{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public GoodsClassfy() { } @Override public String toString() { return "GoodsClassfy{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}'; } }