JDBCTemplate instance

1. Configuration: It is basically the same as the configuration of hibernate and mybatis, the only difference is the configuration of beans. The configuration of JDBCTemplate in spring is:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

<bean id="jdbcTemplate"  
    class="org.springframework.jdbc.core.JdbcTemplate">  
    <property name="dataSource" ref="dataSource"></property>  
</bean>  

2. Use:

@Resource
	private JdbcTemplate jdbcTemplate;

ready to use.

3. Examples:

song:

package com.domain;


public class User {
	
	private String id;
	
	private String ucode;
	
	private String name;
	
	private String sex;

	private String pwd;
	private String sid;
	private String school;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUcode() {
		return ucode;
	}

	public void setUcode(String ucode) {
		this.ucode = ucode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getSid() {
		return sid;
	}
	
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getSchool() {
		return school;
	}
	
	public void setSchool(String school) {
		this.school = school;
	}
	

}

RowMapperUser:

package com.domain;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

 
public class RowMapperUser implements RowMapper<User> {

	
	@Override
	public User mapRow(ResultSet rs, int arg1) throws SQLException {
		User dbuser = new User();
		dbuser.setId(rs.getString("id"));
		dbuser.setName(rs.getString("name"));
		dbuser.setPwd(rs.getString("pwd"));
		dbuser.setSchool(rs.getString("school"));
		dbuser.setSex(rs.getString("sex"));
		dbuser.setSid(rs.getString("sid"));
		dbuser.setUcode(rs.getString("ucode"));
		return dbuser;
	}

}

RowMapperRole:

public class RowMapperRole implements RowMapper<Role> {

	/* (non-Javadoc)
	 * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
	 */
	@Override
	public Role mapRow(ResultSet rs, int arg1) throws SQLException {
		Role role = new Role();
		role.setName(rs.getString("name"));
		role.setRid(rs.getString("rid"));
		return role;
	}

}

dao:

(1.) Add, delete, modify: use the update method, here is just one example:

//increase
public int addUser(User user) {
		
		String sql = "insert into user(id,ucode,name,sex,pwd,sid,school)"
				+ " VALUES (?,?,?,?,?,?,?)";
		int a = jdbcTemplate.update(sql, new Object[]{user.getId(), user.getUcode(),
				user.getName(), user.getSex(), user.getPwd(), user.getSid(),
				user.getSchool()});
		
		return a;
	}

(2,) Query a single field:

public Integer getCount(Map<String, Object> map) {
		Integer count = null;
		Object[] obj=null;
		String role = (String) map.get("role");
		String school = (String) map.get("school");
		String sex = (String) map.get("sex");
		String sql = "select count(*) from "
				+ "(select u.id from user u left join userrole ur "
				+ " on u.id=ur.uid left join role r on r.rid=ur.rid "
				+ " where 1=1 ";
	
		// unconditional query
		if (role == null && school == null && sex == null) {
			System.out.println("Unconditional query");
			sql += "group by u.id) as a";
			obj=new Object[]{};
		}
		// full conditional query
		if (role != null && school != null && sex != null) {
			sql += " and r.name=? and school=? and sex=? group by u.id) as a";
			System.out.println("before full conditional query");
			obj=new Object[]{role, school, sex};
		
			System.out.println("After full conditional query");
		}
		// only check schools
		if (role == null && school != null && sex == null) {
			sql += " and  school=? group by u.id) as a";
			obj=new Object[]{school};
			
		}
		// only check gender
		if (role == null && school == null && sex != null) {
			sql += " and sex=? group by u.id) as a";
			obj=new Object[]{sex};
			
		}
		// only check permissions
		if (role != null && school == null && sex == null) {
			sql += " and r.name=? group by u.id) as a";
			obj=new Object[]{role};
		
		}
		// Check permissions and schools
		if (role != null && school != null && sex == null) {
			sql += " and r.name=? and school=? group by u.id) as a";
			obj=new Object[]{role, school};
			
		}
		// look up school and gender
		if (role == null && school != null && sex != null) {
			sql += "  and school=? and sex=? group by u.id) as a";
			obj=new Object[]{school, sex};
			
		}
		// Check permissions and gender
		if (role != null && school == null && sex != null) {
			sql += "  and r.name=? and sex=? group by u.id) as a";
			obj=new Object[]{role, sex};
			
		}
		count=jdbcTemplate.queryForObject(sql, obj,Integer.class);

		return count;
	}
public String getRidByName(String name) {
		
		String sql = "select rid from role where name=?";
		
		String rid=jdbcTemplate.queryForObject(sql, new Object[]{name},String.class);
		
		return rid;
	}


(3,) Query a single object:

/**
	 * @Title: getUser
	 * @Description: <!--Get logged in user byId-->
	 * @param user
	 * user
	 * @return return user information
	 */
	public User getUser(User user) {
		
		String sql = "select * from user where id=?";
		
		User dbuser =jdbcTemplate.queryForObject(sql, new Object[]{user.getId()},new RowMapperUser());
		
		return dbuser;
	}

(4,) Query object collection:

/**
	 * @Title: getUserRoles
	 * @Description: Get all permissions of the user
	 * @param user
	 * user
	 * @return returns a list of permissions
	 */
	public List<Role> getUserRoles(User user) {
		// new ArrayList<>();
		String sql = "select * from role r left join  "
				+ "userrole ur on r.rid=ur.rid left join user u "
				+ " on ur.uid=u.id where u.id=?";
		List<Role> list = jdbcTemplate.query(sql,
				new Object[] { user.getId() }, new RowMapperRole());

		return list;
	}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325571240&siteId=291194637