【Spring 系列】1. 搭建和配置Spring與jdbc整合的環境
阿新 • • 發佈:2019-01-13
配置資料來源
<!-- 配置結點,可以使用佔位符 --> <context:property-placeholder location=“classpath:jdbc.properties”/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <!-- 連線池啟動時的初始值 --> <property name="initialSize" value="${initialSize}"/> <!-- 連線池的最大值 --> <property name="maxActive" value="${maxActive}"/> <!-- 最大空閒值.當經過一個高峰時間後,連線池可以慢慢將已經用不到的連線慢慢釋放一部分,一直減少到maxIdle為止 --> <property name="maxIdle" value="${maxIdle}"/> <!-- 最小空閒值.當空閒的連線數少於閥值時,連線池就會預申請去一些連線,以免洪峰來時來不及申請 --> <property name="minIdle" value="${minIdle}"/> </bean>
當前類路徑下,儲存jdbc.properties檔案。檔案內容如下:
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
initialSize=1
maxActive=500
maxIdle=2
minIdle=1
配置事務
配置事務時,需要在xml配置檔案中引入用於宣告事務的tx名稱空間
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd事物的配置方式有兩種:註解方式和基於xml配置方式
使用註解方式配置事務
<bean id="personService" class="com.liuyong666.service.impl.PersonServiceBean"> <property name="dataSource" ref="dataSource"/> </bean>
採用@Transactional註解方式使用事務
package com.liuyong666.service.impl; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.annotation.Transactional; import com.liuyong666.bean.Person; import com.liuyong666.service.PersonService; //表明業務方法受spring事務管理 @Transactional public class PersonServiceBean implements PersonService { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource){ this.jdbcTemplate = new JdbcTemplate(dataSource); } public void save(Person person) { jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()}, new int[]{java.sql.Types.VARCHAR}); } public 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}); } public Person getPerson(Integer personid) { return (Person) jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid}, new int[]{java.sql.Types.INTEGER}, new PersonRowMapper()); } public List<Person> getPersons() { return (List<Person>) jdbcTemplate.query("select * from person",new PersonRowMapper()); } public void delete(Integer personid) { jdbcTemplate.update("delete from person where id=?", new Object[]{personid}, new int[]{java.sql.Types.INTEGER}); } }
Person
package com.liuyong666.bean;
public class Person {
private Integer id;
private String name;
public Person() {
}
public Person(String name) {
super();
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PersonRowMapper
package com.liuyong666.service.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.liuyong666.bean.Person;
public class PersonRowMapper implements RowMapper {
//外部預設next
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
}
}
測試
public class PersonServiceTest {
private static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception{
try {
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
personService = (PersonService) context.getBean("personService");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void save(){
personService.save(new Person("小藍"));
}
@Test
public void getPerson(){
Person p = personService.getPerson(1);
System.out.println(p.getName());
}
@Test
public void update(){
Person p = personService.getPerson(1);
p.setName("大明");
personService.update(p);
}
@Test
public void delete(){
personService.delete(1);
}
@Test
public void getBeans(){
for(Person person : personService.getPersons()){
System.out.println(person.getName());
}
}
}
歡迎關注微信公眾號,技術,思維,心理,帶給你認知的全方位成長。
本文由 永倫的小屋 原創。
轉載請註明作者及出處,本文作者為 永倫的小屋。
博文原連結:Spring 系列