多平台统一管理软件接口,如何实现多平台统一管理软件接口
218
2023-06-25
详解spring开发_JDBC操作MySQL数据库
本文介绍spring开发_JDBC操作mysql数据库,具体如下:
项目结构:
数据库表:
/spring_1100_spring+jdbc/src/com/b510/bean/Person.java
package com.b510.bean;
/**
* 普通的javaBean类Person
*
* @author Hongten
*
*/
public class Person {
/**
* id号
*/
private int id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 性别
*/
private String sex;
public Person(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Person() {
}
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;
}
}
/spring_1100_spring+jdbc/src/com/b510/service/PersonService.java
package com.b510.service;
import java.util.List;
import com.b510.bean.Person;
public interface PersonService {
/**
* 保存Person
*
* @param person
*/
public abstract void save(Person person);
/**
* 更新Person
*
* @param person
*/
public abstract void update(Person person);
/**
* 获取Person
*
* @param id
* @return
*/
public abstract Person getPerson(Integer id);
/**
* 获取所有Person
*
* @return
*/
public abstract List
/**
* 删除指定id的Person
*
* @param id
*/
public abstract void delete(Integer id);
}
/spring_1100_spring+jdbc/src/com/b510/service/impl/PersonServiceBean.java
package com.b510.service.impl;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.b510.bean.Person;
import com.b510.service.PersonService;
/**
* 业务bean
*
* @author Hongten
*
*/
public class PersonServiceBean implements PersonService {
/**
* 数据源
*/
private DataSource dataSource;
/**
* spring提供的jdbc操作辅助类
*/
private JdbcTemplate jdbcTemplate;
// 设置数据源
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void save(Person person) {
jdbcTemplate.update("insert into person(name,age,sex)values(?,?,?)",
new Object[] { person.getName(), person.getAge(),
person.getSex() }, new int[] { java.sql.Types.VARCHAR,
java.sql.Types.INTEGER, java.sql.Types.VARCHAR });
}
public void update(Person person) {
jdbcTemplate.update("update person set name=?,age=?,sex=? where id=?",
new Object[] { person.getName(), person.getAge(),
person.getSex(), person.getId() }, new int[] {
java.sql.Types.VARCHAR, java.sql.Types.INTEGER,
java.sql.Types.VARCHAR, java.sql.Types.INTEGER });
}
public Person getPerson(Integer id) {
Person person = (Person) jdbcTemplate.queryForObject(
"select * from person where id=?", new Object[] { id },
new int[] { java.sql.Types.INTEGER }, new PersonRowMapper());
return person;
}
@SuppressWarnings("unchecked")
public List
List
return list;
}
public void delete(Integer id) {
jdbcTemplate.update("delete from person where id = ?", new Object[] { id },
new int[] { java.sql.Types.INTEGER });
}
}
/spring_1100_spring+jdbc/src/com/b510/service/impl/PersonRowMapper.java
package com.b510.service.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.b510.bean.Person;
public class PersonRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet set, int index) throws SQLException {
Person person = new Person(set.getInt("id"), set.getString("name"), set
.getInt("age"), set.getString("sex"));
return person;
}
}
/spring_1100_spring+jdbc/src/com/b510/test/SpringJDBCTest.java
package com.b510.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.b510.bean.Person;
import com.b510.service.PersonService;
public class SpringJDBCTest {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
PersonService personService = (PersonService) act
.getBean("personService");
Person person = new Person();
person.setName("苏东坡");
person.setAge(21);
person.setSex("男");
// 保存一条记录
personService.save(person);
List
System.out.println("++++++++得到所有Person");
for (Person person2 : person1) {
System.out.println(person2.getId() + " " + person2.getName()
+ " " + person2.getAge() + " " + person2.getSex());
}
Person updatePerson = new Person();
updatePerson.setName("Divide");
updatePerson.setAge(20);
updatePerson.setSex("男");
updatePerson.setId(5);
// 更新一条记录
personService.update(updatePerson);
System.out.println("******************");
// 获取一条记录
Person onePerson = personService.getPerson(2);
System.out.println(onePerson.getId() + " " + onePerson.getName()
+ " " + onePerson.getAge() + " " + onePerson.getSex());
// 删除一条记录
personService.delete(1);
}
}
/spring_1100_spring+jdbc/src/bean.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd"> destroy-method="close"> value="jdbc:mysql://localhost:3307/spring?useUnicode=true&characterEncoding=UTF-8" /> class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-2.5.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">
destroy-method="close"> value="jdbc:mysql://localhost:3307/spring?useUnicode=true&characterEncoding=UTF-8" />
destroy-method="close">
value="jdbc:mysql://localhost:3307/spring?useUnicode=true&characterEncoding=UTF-8" />
value="jdbc:mysql://localhost:3307/spring?useUnicode=true&characterEncoding=UTF-8" />
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
运行结果;
2012-3-9 23:30:57 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a05308: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]; startup date [Fri Mar 09 23:30:57 CST 2012]; root of context hierarchy
2012-3-9 23:30:57 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
2012-3-9 23:30:58 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2bb514
2012-3-9 23:30:58 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2bb514: defining beans [dataSource,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,personService]; root of factory hierarchy
++++++++得到所有Person
2 TomCat 12 女
3 hongten 21 男
4 liufang 21 女
5 Divide 20 男
6 Jone 20 女
7 苏东坡 21 男
******************
2 TomCat 12 女
当然我们可以用配置文件来存放我们的数据源信息:
/spring_1100_spring+jdbc/src/jdbc.properties
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc\:mysql://localhost\:3307/spring?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=root
initialSize=1
maxActive=300
maxIdle=2
minIdle=1
相应要修改:
/spring_1100_spring+jdbc/src/bean.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd"> destroy-method="close"> class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-2.5.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">
destroy-method="close">
destroy-method="close">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
运行结果是相同的:
2012-3-10 0:23:59 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Sat Mar 10 00:23:59 CST 2012]; root of context hierarchy
2012-3-10 0:23:59 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
2012-3-10 0:23:59 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1aa57fb
2012-3-10 0:23:59 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [jdbc.properties]
2012-3-10 0:23:59 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1aa57fb: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,personService]; root of factory hierarchy
++++++++得到所有Person
2 TomCat 12 女
3 hongten 21 男
4 liufang 21 女
5 Divide 20 男
6 Jone 20 女
7 苏东坡 21 男
8 苏东坡 21 男
******************
2 TomCat 12 女
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~