java

位置:IT落伍者 >> java >> 浏览文章

Spring 数据源配置与应用


发布日期:2020年08月28日
 
Spring 数据源配置与应用

Spring 数据源配置与应用

Spring对数据库操作都依赖数据源

Spring有默认的数据源实现orgspringframeworkjdbcdatasourceDriverManagerDataSource但也可以配置其他的数据源实现比如DBCP的数据源public class BasicDataSource implements javaxsqlDataSource

一旦获取到数据源DataSource实例就可以通过DataSource获取到数据库连接操作数据库

下面是Spring数据源的一个简单配置和应用

应用环境MySQL

drop table if exists user;

/*==============================================================*/

/* Table: user*/

/*==============================================================*/

create table user

(

id bigint AUTO_INCREMENT not null

name varchar()

ageint

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) {

thisid = id;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

thisage = age;

}

}

public interface IUserDAO {

public void insert(User user);

public User find(Integer id);

}

/**

* Created by IntelliJ IDEA<br>

* <b>User</b>: leizhimin<br>

* <b>Date</b>: ::<br>

* <b>Note</b>: 子类DAO

*/

public class UserDAO extends BaseDAO implements IUserDAO {

public void insert(User user) {

String name = usergetName();

int age = usergetAge()intValue();

Connection conn = null;

Statement stmt = null;

try {

conn = getConnection();

stmt = conncreateStatement();

stmtexecute(INSERT INTO user (nameage) + VALUES( + name + + age + ));

} catch (SQLException e) {

eprintStackTrace();

}

finally {

if (stmt != null) {

try {

stmtclose();

}

catch (SQLException e) {

eprintStackTrace();

}

}

if (conn != null) {

try {

connclose();

}

catch (SQLException e) {

eprintStackTrace();

}

}

}

}

public User find(Integer id) {

Connection conn = null;

Statement stmt = null;

try {

conn = getConnection();

stmt = conncreateStatement();

ResultSet result = stmtexecuteQuery(

SELECT * FROM user WHERE id= + idintValue());

if (resultnext()) {

Integer i = new Integer(resultgetInt());

String name = resultgetString();

Integer age = new Integer(resultgetInt());

User user = new User();

usersetId(i);

usersetName(name);

usersetAge(age);

return user;

}

} catch (SQLException e) {

eprintStackTrace();

}

finally {

if (stmt != null) {

try {

stmtclose();

}

catch (SQLException e) {

eprintStackTrace();

}

}

if (conn != null) {

try {

connclose();

}

catch (SQLException e) {

eprintStackTrace();

}

}

}

return null;

}

}

/**

* Created by IntelliJ IDEA<br>

* <b>User</b>: leizhimin<br>

* <b>Date</b>: ::<br>

* <b>Note</b>: 基类DAO提供了数据源注入

*/

public class BaseDAO {

private DataSource dataSource;

public DataSource getDataSource() {

return dataSource;

}

public void setDataSource(DataSource dataSource) {

thisdataSource = dataSource;

}

public Connection getConnection() {

Connection conn = null;

try {

conn = dataSourcegetConnection();

} catch (SQLException e) {

eprintStackTrace();

}

return conn;

}

}

<?xml version= encoding=UTF?>

<!DOCTYPE beans PUBLIC //SPRING/DTD BEAN/EN

beansdtd>

<beans>

<bean id=dataSource

class=orgspringframeworkjdbcdatasourceDriverManagerDataSource>

<property name=driverClassName>

<value>commysqljdbcDriver</value>

</property>

<property name=url>

<value>jdbc:mysql://localhost:/springdb</value>

</property>

<property name=username>

<value>root</value>

</property>

<property name=password>

<value>leizhimin</value>

</property>

</bean>

<bean id=baseDAO class=comlavasoftspringnotech_jdbcBaseDAO abstract=true>

<property name=dataSource>

<ref bean=dataSource/>

</property>

</bean>

<bean id=userDAO

class=comlavasoftspringnotech_jdbcUserDAO parent=baseDAO>

</bean>

</beans>

/**

* Created by IntelliJ IDEA<br>

* <b>User</b>: leizhimin<br>

* <b>Date</b>: ::<br>

* <b>Note</b>: 客户端测试

*/

public class SpringDAODemo {

public static void main(String[] args) {

ApplicationContext context = new FileSystemXmlApplicationContext(D:\\_spring\\src\\com\\lavasoft\\springnote\\ch_jdbc\\beanjdbcxml);

User user = new User();

usersetName(caterpillar);

usersetAge(new Integer());

IUserDAO userDAO = (IUserDAO) contextgetBean(userDAO);

userDAOinsert(user);

user = userDAOfind(new Integer());

Systemoutprintln(name: + usergetName());

}

}

运行结果

logj:WARN No appenders could be found for logger (orgreCollectionFactory)

logj:WARN Please initialize the logj system properly

name: jdbctemplate

Process finished with exit code

注意Spring配置文件中对继承的配置DataSource注入方式通过继承来注入从而简化编程

上面用的是Spring的自己的数据源实现现在假如要换成apache的DBCP数据源则配置改为如下即可

<bean id=dataSource

class=monsdbcpBasicDataSource singleton=true>

<property name=driverClassName>

<value>commysqljdbcDriver</value>

</property>

<property name=url>

<value>jdbc:mysql://localhost:/springdb</value>

</property>

<property name=username>

<value>root</value>

</property>

<property name=password>

<value>leizhimin</value>

</property>

</bean>

实际上仅仅是更改一下数据源的calss实现

               

上一篇:Hibernate Search:全文检索你的领域模型

下一篇:将eclipse创建的java转化为web项目