天天看點

spring+jdbc模闆

spring+jdbc模闆

準備工作

準備工具類和檔案

1、*.properties檔案

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ums
userName=root
password=123456
           
2、PropertiesUtils.java
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
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;
    }
}
           
建立GoodsClassfy實體類;GoodsClassfy.java
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 + '\'' +
                '}';
    }
}
           

繼續閱讀