Spring 數據源配置與應用
Spring對數據庫操作都依賴數據源
Spring有默認的數據源實現org
一旦獲取到數據源DataSource實例
下面是Spring數據源的一個簡單配置和應用
應用環境
drop table if exists user;
/*==============================================================*/
/* Table: user */
/*==============================================================*/
create table user
(
id bigint AUTO_INCREMENT not null
name varchar(
age int
primary key (id)
);
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this
}
}
public interface IUserDAO {
public void insert(User user);
public User find(Integer id);
}
/**
* Created by IntelliJ IDEA
* <b>User</b>: leizhimin<br>
* <b>Date</b>:
* <b>Note</b>: 子類DAO
*/
public class UserDAO extends BaseDAO implements IUserDAO {
public void insert(User user) {
String name = user
int age = user
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn
stmt
} catch (SQLException e) {
e
}
finally {
if (stmt != null) {
try {
stmt
}
catch (SQLException e) {
e
}
}
if (conn != null) {
try {
conn
}
catch (SQLException e) {
e
}
}
}
}
public User find(Integer id) {
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn
ResultSet result = stmt
if (result
Integer i = new Integer(result
String name = result
Integer age = new Integer(result
User user = new User();
user
user
user
return user;
}
} catch (SQLException e) {
e
}
finally {
if (stmt != null) {
try {
stmt
}
catch (SQLException e) {
e
}
}
if (conn != null) {
try {
conn
}
catch (SQLException e) {
e
}
}
}
return null;
}
}
/**
* Created by IntelliJ IDEA
* <b>User</b>: leizhimin<br>
* <b>Date</b>:
* <b>Note</b>: 基類DAO
*/
public class BaseDAO {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this
}
public Connection getConnection() {
Connection conn = null;
try {
conn = dataSource
} catch (SQLException e) {
e
}
return conn;
}
}
<?xml version=
<!DOCTYPE beans PUBLIC
<beans>
<bean id=
class=
<property name=
<value>com
</property>
<property name=
<value>jdbc:mysql://localhost:
</property>
<property name=
<value>root</value>
</property>
<property name=
<value>leizhimin</value>
</property>
</bean>
<bean id=
<property name=
<ref bean=
</property>
</bean>
<bean id=
class=
</bean>
</beans>
/**
* Created by IntelliJ IDEA
* <b>User</b>: leizhimin<br>
* <b>Date</b>:
* <b>Note</b>: 客戶端測試
*/
public class SpringDAODemo {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext(
User user = new User();
user
user
IUserDAO userDAO = (IUserDAO) context
userDAO
user = userDAO
System
}
}
運行結果
log
log
name: jdbctemplate
Process finished with exit code
注意
上面用的是Spring的自己的數據源實現
<bean id=
class=
<property name=
<value>com
</property>
<property name=
<value>jdbc:mysql://localhost:
</property>
<property name=
<value>root</value>
</property>
<property name=
<value>leizhimin</value>
</property>
</bean>
實際上僅僅是更改一下數據源的calss實現
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27993.html