Spring JdbcTemplate笔记
来源:互联网 发布:手机淘宝店铺上货教程 编辑:程序博客网 时间:2024/05/16 05:12
xml配置文件
<context:annotation-config /> //加入注解<context:component-scan base-package="foo.bar"/> //批量扫描该包下的类<context:property-placeholder location="dp.properties"></context:property-placeholder> //加载数据库密码等文件<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property></bean>//配置jdbcTemplate<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">//载入数据源
<property name="dataSource" ref="dataSource"></property></bean>
测试类
public static void main(String[] args) { // ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); //加载配置文件 // JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate"); //获得数据库连接 // String sql ="update items set name=? where id =?"; // jdbcTemplate.update(sql,"tezhongbing",2); //根据数据库数据 //HelloService helloService = context.getBean(HelloService.class); // System.out.println(helloService.sayHello()); // String sql = "INSERT INTO items(name,price,detail,pic,createtime)values(?,?,?,?,?)"; //插入数据 // List<Object[]> batchArgs = new ArrayList<Object[]>();//插入多条数据 // batchArgs.add(new Object[]{"chen", 12, "tinghaode","a",new Date()}); // batchArgs.add(new Object[]{"jun", 14, "tinghaode", "b", new Date()}); // batchArgs.add(new Object[]{"wei", 34, "tinghaode", "c", new Date()}); // jdbcTemplate.batchUpdate(sql, batchArgs); // String sql ="SELECT id,name,price,pic,createtime createTime FROM items WHERE id =?"; //RowMapper<Items> rowMapper =new BeanPropertyRowMapper<Items>(Items.class); //Items items = jdbcTemplate.queryForObject(sql,rowMapper,2); //System.out.println(items);}
编写单测代码
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:spring-config.xml") //用注解载入配置文件public class SpringAppTests { @Autowired //自动装载对象 private HelloService helloService; @Autowired private ItemsDao itemsDao; @Test //单测标记 public void testSayHello() { Assert.assertEquals("Hello world!", helloService.sayHello()); } @Test public void testInsert(){ System.out.println(itemsDao.getcont(5)); }}
编写查询Dao
@Repository //注解注入public class ItemsDao { @Autowired //自动注入jdbcTemplate JdbcTemplate jdbcTemplate; public List<Items> getcont(Integer id){ String sql ="SELECT id,name,price,pic,createtime createTime FROM items WHERE price ='12.0' " + "and id < ? "; RowMapper<Items> rowMapper =new BeanPropertyRowMapper<Items>(Items.class); List<Items> items = jdbcTemplate.query(sql,rowMapper,6); return items; }}
0 0
- Spring JdbcTemplate学习笔记
- Spring JdbcTemplate笔记
- Spring JDBCTemplate学习笔记
- spring-JdbcTemplate用法笔记
- spring学习笔记十七 JDBCTemplate
- Spring MVC笔记 使用JdbcTemplate
- spring学习笔记 -- day10 spring中的jdbcTemplate
- spring JdbcTemplate
- spring jdbcTemplate
- Spring JdbcTemplate
- Spring JDBCTemplate
- Spring jdbctemplate
- spring JdbcTemplate
- spring jdbcTemplate
- Spring JDBCTemplate
- spring jdbctemplate
- spring jdbctemplate
- Spring JdbcTemplate
- PC是什么 PC是什么意思?
- Seeding
- 打开系统设置界面
- 对字典进行排序
- 深入浅出Gradle 整理
- Spring JdbcTemplate笔记
- 回溯法应用:整数拆分
- 为什么选择Nginx
- Avoid The Lakes
- 如何加密文件夹
- jQuery 参考手册 - 选择器
- Object分析--1、clone()方法
- Block使用详解,Block与代理相比的优点与缺点
- Java8中用sun.misc.Contended避免伪共享(false sharing)