侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

JDBC

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 105 阅读 / 5,161 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
jdbc增强类,少写代码。
spring提供了一个可以操作数据库的对象,模板类
JDBCTemplate
	com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
	mysql-connector-java-5.1.40-bin.jar
	spring-jdbc-4.2.4.RELEASE.jar
	spring-tx-4.2.4.RELEASE.jar</code></pre></div>            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
            <div style="color: inherit; fill: inherit;">
                <div style="display: flex;">

import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Demo1 {
public static void main(String[] args) throws Exception {
//创建连接池对象
ComboPooledDataSource ds = new ComboPooledDataSource();
//配置数据源的四大参数
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/dianshan");
ds.setUser("root");
ds.setPassword("1234");

	//创建模板对象
	JdbcTemplate jt = new JdbcTemplate(ds);
	
	String sql = &quot;insert into t_user values(1,'rose')&quot;;
	
	//操作数据
	jt.execute(sql);
}

}

<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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--  指定spring使用的配置文件  -->
<context:property-placeholder location="classpath:db.properties"/>
 <!-- 开启注解扫描 -->
<context:component-scan base-package="com.yh"/>

 <!--  配置连接池对象  -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="${driverClass}"/>
	<property name="jdbcUrl" value="${jdbcUrl}"/>
	<property name="user" value="${user}"/>
	<property name="password" value="${password}"/>
</bean>

 <!--  配置JdbcTemplate模板对象,将DataScource注入模板对象中  
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"></property>
</bean>  
-->
 <!--  配置UserDao,将模板对象注入  -->
<bean id="ud" class="com.yh.dao.UserDaoImpl">
 <!--  <property name="jt" ref="jt"></property>  -->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/dianshan
user=root
password=1234
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.xml.Jdbc4SqlXmlHandler;

import com.yh.bean.User;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
	@Autowired
  private ComboPooledDataSource dataSource;

  @PostConstruct
  private void init(){
    super.setDataSource(dataSource);
  }
	@Override
	public void save(User u) {		
		String sql = "insert into t_user values(?,?)";
		super.getJdbcTemplate().update(sql, u.getId(),u.getName());
	}

	@Override
	public void delete(int id) {
		super.getJdbcTemplate().update("delete from t_user where id = ?", id);
	}

	@Override
	public void update(User u) {
		super.getJdbcTemplate().update("update t_user set name = ? where id = ?", u.getName(),u.getId());
	}

	@Override
	public User getUserById(int id) {
		String sql = "select * from t_user where id = ?";
		
		return super.getJdbcTemplate().queryForObject(sql, new RowMapper<User>(){

			@Override
			public User mapRow(ResultSet arg0, int arg1) throws SQLException {
				User user = new User();
				user.setId(arg0.getInt(1));
				user.setName(arg0.getString(2));
				return user;
			}
			
		}, id);
	}

	@Override
	public List<User> getAll() {
		String sql = "select * from t_user";
		return super.getJdbcTemplate().query(sql, new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet arg0, int arg1) throws SQLException {
				User user = new User();
				user.setId(arg0.getInt(1));
				user.setName(arg0.getString(2));
				return user;
			}
		});
	}

	@Override
	public int getUserCount() {
		String sql = "select count(*) from t_user";
		return super.getJdbcTemplate().queryForObject(sql, Integer.class);
	}
}
import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.yh.bean.User;
import com.yh.dao.UserDao;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo2 {
	
	@Resource(name="ud")
	private UserDao dao;
	
	@Test
	public void test1(){
		User u =  new User(2,"tony");
		
		dao.save(u);
	}
	
	@Test
	public void test2(){
		dao.delete(2);
	}
	
	@Test
	public void test3(){
		System.out.println(dao.getUserById(1));
	}
	
	
	@Test
	public void test4(){
		System.out.println(dao.getUserCount());
	}
	
	
	@Test
	public void test5(){
		System.out.println(dao.getAll());
	}
}
            <div style="color: inherit; fill: inherit;">
                <div style="display: flex;">

0

评论区