新建一个java工程 写好spring配置文件,直接上代码

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="db.properties"></context:property-placeholder> <!-- 导入C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.Username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.Url}"></property>
<property name="driverClass" value="${jdbc.Driver}"></property>
</bean> <!-- 配置Spring JdbcTemplate -->
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>

不知道什么意思可以看上面的注解,

接下来我们写好我们数据库的基本配置文件

jdbc.Driver=com.mysql.jdbc.Driver
jdbc.Username=root
jdbc.password=root
jdbc.Url=JDBC:mysql://127.0.0.1:3306/mydata

这里的话是配合上面c3p0数据库使用的,以便可以成功读取到数据库。

接下来我们再写一个实体类,后面用到查询数据的时候会用到实体类。

package com.xiaojiang.template;

public class MyJDBCData {

    private int id;
private String name;
private int age;
private String sex; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "MyJDBCData [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
} }

数据库和数据表自己去新建,然后再根据自己创建的字段名来写这个实体类。

接下来我们上测试代码

package com.xiaojiang.template;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.List; import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; class jdbcTest { private String sql [] = {
"insert into attributes(id,name,age,sex) values(?,?,?,?)",
"update attributes set name = ? where id = ?",
"delete from attributes where id = ?",
"select *from attributes where id = ?",
"select *from attributes where id >= ?",
"select count(id) attributes from attributes"
}; private ApplicationContext ac = null;
private JdbcTemplate p = null; {
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
p = (JdbcTemplate) ac.getBean("JdbcTemplate");
} //查询多条数据
@Test
public void testQueryForList()
{
RowMapper<MyJDBCData> rowMapper = new BeanPropertyRowMapper<>(MyJDBCData.class);
List<MyJDBCData> data = p.query(sql[4], rowMapper,1);
System.out.println(data);
} //获取数据库指定的数据,并得到一个对象
@Test
public void testQueryForObject()
{
//把查询结果转换成一个实体
RowMapper<MyJDBCData> rowMapper = new BeanPropertyRowMapper<>(MyJDBCData.class);
MyJDBCData data = p.queryForObject(sql[3],rowMapper,1);
System.out.println(data.toString());
} //批量增加数据
@Test
public void testbatchData()
{
List<Object[]> batchArgs = new ArrayList<>();
batchArgs.add(new Object[]{"1","测试1","18","男"});
batchArgs.add(new Object[]{"2","测试2","19","男"});
batchArgs.add(new Object[] {"3","测试3","20","女"});
p.batchUpdate(sql[0], batchArgs);
}
//查询单个列的值,做统计查询
@Test
public void testQueryForObject1()
{
Long count = p.queryForObject(sql[5],Long.class);
System.out.println(count);
} //执行单条 INSERT UPDATE DELETE
@Test
public void testInsert()
{
p.update(sql[0],"1","小江","18","男");
}
//修改
@Test
public void testUpdate()
{
p.update(sql[1],"小红",1);
}
//删除
@Test
public void testDelete()
{
p.update(sql[2],1);
} @Test
void test() {
fail("Not yet implemented");
} }

我们在用的时候最好把他写成一个类,要用到谁的时候就去调用就行了,我这里只是测试一下,就不多写了,写的简单不喜勿喷。

最新文章

  1. NSURLCache
  2. Python分割list
  3. 黄聪:PHP解决textarea内容换行存入数据库,如何解析取出不能自动换行
  4. VS2010中使用GDAL(一)
  5. [iOS]关于视频方向的若干问题
  6. Let the Balloon Rise
  7. 使用memcached实现tomcat集群session共享
  8. Spring JTA应用JOTM &amp; Atomikos II JOTM
  9. html 5 drag and drop upload file
  10. codeforces C. Inna and Huge Candy Matrix
  11. Android---&gt;Button按钮操作
  12. 2243: [SDOI2011]染色
  13. 关于在&quot;a&quot;标签中添加点击事件的一些问题
  14. (转)C#中各种集合类比较
  15. javascript获取表单的各项值
  16. Vue CLI 3+tinymce 5富文本编辑器整合
  17. SQL SERVER镜像配置(包含见证服务器)
  18. JAVA中native方法调用
  19. 查看加密的vba代码
  20. 20172313『Java程序设计』课程结对编程练习_四则运算第二周阶段总结

热门文章

  1. oracle的用户、权限、表空间的管理
  2. JavaSE--[转]加密和签名的区别
  3. Django专题之ORM操作2
  4. 帝国CMS7.5后台美化模板 后台风格修改 帝国CMS后台模板
  5. 获取文件MD5值(JS、JAVA)
  6. Proe 导出PDF Vb.net
  7. 日期控件 My97DatePicker WdatePicker 日期格式
  8. Android开发学习1----AndroidStudio的安装、创建第一个Android Studio文件、Android Studio界面介绍和HelloWord!
  9. The website is API(3)
  10. jumpserver_跳板机实操