spring学习笔记3

来源:互联网 发布:d3.js transform 编辑:程序博客网 时间:2024/05/16 09:43
package zmj.service.interf;import java.util.List;import zmj.bean.Person;;public interface PersonService {/** * 保存person * @param person */public void save(Person person);/** * 更新person * @param person */public void update(Person person);/** * 获取person * @param personId * @return */public Person getPerson(Integer personId);/** * 获取所有的person集合 * @return */public List<Person> getPersons();/** * 根据id来删除记录 * @param personId */public void delete(Integer personId);}package zmj.service.impl;/*import java.util.HashMap;import java.util.List;import java.util.ArrayList;import java.util.HashSet;import java.util.Map;import java.util.Properties;import java.util.Set;*/import java.util.List;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import zmj.bean.Person;import zmj.dao.interf.PersonDao;import zmj.service.interf.PersonService;@Transactionalpublic class PersonServiceBean implements PersonService {/*private DataSource dataSource;*/private JdbcTemplate jdbcTemplate;//辅助类,对jdbc进行操作public void setDataSource(DataSource dataSource) {    //通过setter类进行注入this.jdbcTemplate = new JdbcTemplate(dataSource);}@Overridepublic void save(Person person) {jdbcTemplate.update("insert into person(name) values(?)", new Object[] {person.getName()}, new int[] {java.sql.Types.VARCHAR});}@Overridepublic void update(Person person) {jdbcTemplate.update("update person set name=? where id=?", new Object[] {person.getName(),person.getId()}, new int[] {java.sql.Types.VARCHAR,java.sql.Types.INTEGER});}@Overridepublic Person getPerson(Integer personId) {return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personId}, new PersonRowMapper());}@SuppressWarnings("unchecked")@Overridepublic List<Person> getPersons() {return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper());}@Overridepublic void delete(Integer personId) {jdbcTemplate.update("delete from person where id=?", new Object[] {personId}, new int[] {java.sql.Types.INTEGER});}}package zmj.bean;public class Person {private String name;private Integer id;public Person() {}public Person(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}}package zmj.service.impl;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;import zmj.bean.Person;public class PersonRowMapper implements RowMapper {//getPerson() 和getPersons()的回调方法@Overridepublic Object mapRow(ResultSet rs, int index) throws SQLException {Person person = new Person(rs.getString("name"));person.setId(rs.getInt("id"));return person;}}package junit_test;import static org.junit.Assert.*;import java.util.List;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import zmj.bean.Person;import zmj.service.interf.PersonService;public class springTest {private static PersonService personService;@BeforeClasspublic static void setUpBeforeClass() throws Exception{try {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");personService = (PersonService)ctx.getBean("personService");}catch (RuntimeException e){e.printStackTrace();}}@Testpublic void save() {personService.save(new Person("yes"));}/*@Testpublic void getPerson() {Person person = personService.getPerson(1);System.out.println("name: " + person.getName());}*//*@Testpublic void update() {Person person = personService.getPerson(2);person.setName("Dog");personService.update(person);}*//*@Testpublic void delete() {personService.delete(1);}*//*@Testpublic void getPersons() {List<Person> persons = personService.getPersons();for(Person person : persons)System.out.println(person.getName());}*/}<?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">           <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>    <property name="url" value="jdbc:mysql://127.0.0.1:3306/zmj"></property>    <property name="username" value="XXX"></property>    <property name="password" value="XXX"></property>        <!--连接池启动的初始值  -->    <property name="initialSize" value="1"></property>        <property name="maxActive" value="500"></property>        <property name="maxIdle" value="2"></property>        <property name="minIdle" value="1"></property>    </bean><!-- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><tx:annotation-driven transaction-manager="txManager"/> -->
<!--将数据源注入到personService中-->
<bean id="personService" class="zmj.service.impl.PersonServiceBean"><property name="dataSource" ref="dataSource"></property></bean></beans>