Java开发之Spring连接数据库方法实例分析

作者:烟大洋仔 时间:2024-01-26 02:00:54 

本文实例讲述了Java开发之Spring连接数据库方法。分享给大家供大家参考,具体如下:
接口:


package cn.com.service;
import java.util.List;
import cn.com.bean.PersonBean;
public interface PersonService {
//保存
public void save(PersonBean person);
//更新
public void update(PersonBean person);
//获取person
public PersonBean getPerson(int id);
public List<PersonBean> getPersonBean();
//删除记录
public void delete(int personid);
}

Person Bean类:


package cn.com.bean;
public class PersonBean {
private int id;
private String name;
public PersonBean(String name) {
 this.name=name;
}
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;
}
}

接口实现:


package cn.com.service.impl;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import cn.com.bean.PersonBean;
import cn.com.service.PersonService;
public class PersonServiceImpl implements PersonService {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
 this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public void save(PersonBean person) {
 // TODO Auto-generated method stub
 jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()},
   new int[]{java.sql.Types.VARCHAR});
}
@Override
public void update(PersonBean person) {
 // TODO Auto-generated method stub
 jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(),person.getId()},
   new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
}
@Override
public PersonBean getPerson(int id) {
 // TODO Auto-generated method stub
 return (PersonBean)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{id},
   new int[]{java.sql.Types.INTEGER},new PersonRowMapper() );
}
@SuppressWarnings("unchecked")
@Override
public List<PersonBean> getPersonBean() {
 // TODO Auto-generated method stub
 return (List<PersonBean>)jdbcTemplate.query("select * from person",
   new PersonRowMapper() );
}
@Override
public void delete(int personid) {
 // TODO Auto-generated method stub
 jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
   new int[]{java.sql.Types.INTEGER});
}
}

RowMapper:


package cn.com.service.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import cn.com.bean.PersonBean;
public class PersonRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet rs, int index) throws SQLException {
 // TODO Auto-generated method stub
 PersonBean person =new PersonBean(rs.getString("name"));
 person.setId(rs.getInt("id"));
 return person;
}
}

beans.xml配置


<?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"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  ">  
   <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/wy"/>
   <property name="username" value="root"/>
   <!-- property池启动时的初始值 -->
    <property name="password" value="123"/>
    <!-- 连接name="initialSize" value="${initialSize}"/>-->
    <property name="initialSize" value="1"/>
    <!-- 连接池的最大值 -->
    <property name="maxActive" value="500"/>
    <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
    <property name="maxIdle" value="2"/>
    <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
    <property name="minIdle" value="1"/>
   </bean>
   <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
   </bean>
   <tx:annotation-driven transaction-manager="txManager"/>
   <bean id="personService" class="cn.com.service.impl.PersonServiceImpl">
   <property name="dataSource" ref="dataSource"></property>
   </bean>
</beans>

测试类:


package Junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.bean.PersonBean;
import cn.com.service.PersonService;
public class PersonTest2 {
private static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");
personService=(PersonService) act.getBean("personService");
}
@Test
public void save() {
personService.save(new PersonBean("wyy"));
}
@Test
public void update() {
PersonBean person=personService.getPerson(1);
person.setName("wy");
personService.update(person);
}
@Test
public void getPerson() {
PersonBean person=personService.getPerson(1);
System.out.println(person.getName());
}
@Test
public void delete() {
personService.delete(1);
}
}

数据库:


Create Table
CREATE TABLE `person` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

希望本文所述对大家Java程序设计有所帮助。

标签:Java,Spring,数据库
0
投稿

猜你喜欢

  • JavaScript 模拟类机制及私有变量的方法及思路

    2024-05-03 15:04:55
  • ASP使用FSO组件生成HTML静态页面

    2007-10-15 12:19:00
  • PyQt中实现自定义工具提示ToolTip的方法详解

    2023-11-09 13:34:56
  • 关于Keras模型可视化教程及关键问题的解决

    2021-03-19 10:39:07
  • Python中.py文件打包成exe可执行文件详解

    2023-06-10 19:11:00
  • Python中数组切片的用法实例详解

    2022-09-18 16:51:42
  • Oracle数据库游标使用大全

    2008-03-04 18:24:00
  • MySQL高效导入多个.sql文件方法详解

    2024-01-20 19:10:44
  • golang 中 channel 的详细使用、使用注意事项及死锁问题解析

    2024-04-26 17:26:45
  • python 队列基本定义与使用方法【初始化、赋值、判断等】

    2023-08-13 06:34:46
  • 解决MSSQL2005远程连接sql2000非默认端口数据库的问题

    2024-01-28 09:41:10
  • Python将脚本程序转变为可执行程序的实现

    2022-03-02 16:30:49
  • mysql如何配置白名单访问

    2024-01-25 15:43:20
  • 设计之外随谈

    2009-06-16 14:38:00
  • oracle 时间格式的调整

    2009-05-24 19:32:00
  • MySQL面试题讲解之如何设置Hash索引

    2024-01-21 17:23:55
  • Python 列表映射后的平均值

    2021-12-25 19:02:39
  • 用javascript实现Base64编码

    2008-03-04 16:51:00
  • Python中eval函数的表达式作用示例

    2022-06-14 02:18:32
  • Python实现ping指定IP的示例

    2023-10-05 04:20:10
  • asp之家 网络编程 m.aspxhome.com