1:如果沒有用註解
1)目錄結構
2)spring及db配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<bean id="studentService" class="com.csdn.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
<bean id="studentDao" class="com.csdn.dao.impl.StudentDaoImpl">
<property name="queryRunner" ref="queryRunner"/>
</bean>
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<context:property-placeholder location="classpath*:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
</beans>
db.perproties
db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql:///student?useUnicode=true&characterEncoding=utf-8
db.username = root
db.password = 123456
3)部分源碼
StudentDaoImpl.java
package com.csdn.dao.impl;
import com.csdn.dao.StudentDao;
import com.csdn.pojo.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
private QueryRunner queryRunner;
public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}
@Override
public List<Student> queryList() throws SQLException {
String sql = "select * from student";
return queryRunner.query(sql,new BeanListHandler<>(Student.class));
}
}
StudentServiceImpl.java
package com.csdn.service.impl;
import com.csdn.dao.StudentDao;
import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import java.sql.SQLException;
import java.util.List;
public class StudentServiceImpl implements StudentService {
StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public List<Student> findAll() throws SQLException {
return studentDao.queryList();
}
}
Test.java 測試代碼
package com.csdn.test;
import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.SQLException;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test {
@Autowired
private StudentService studentService;
@org.junit.Test
public void test01(){
List<Student> all = null;
try {
System.out.println(studentService);
all = studentService.findAll();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(all);
}
}
2:spring註解
1)將javabean放到spring容器中
@Repository 一般用於dao層
@Service 一般用於service層
@Controller 一般用於controller層
@Component 通用
2) 從spring容器中取出javabean對象
@Autowired 根據類型取
@Resource 根據名稱取
3)@Scope註解
@scope值爲singleton,prototype,request,session
4)@PostConstruct @Predestroy
5)註解示例
spring2.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!--掃描有註解的包-->
<context:component-scan base-package="com.csdn.*"/>
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<context:property-placeholder location="classpath*:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
</beans>
StudentDaoImpl2.java
package com.csdn.dao.impl;
import com.csdn.dao.StudentDao;
import com.csdn.pojo.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.List;
@Repository
public class StudentDaoImpl2 implements StudentDao {
@Autowired
private QueryRunner queryRunner;
@Override
public List<Student> queryList() throws SQLException {
String sql = "select * from student";
return queryRunner.query(sql,new BeanListHandler<>(Student.class));
}
}
StudentServiceImpl2.java
package com.csdn.service.impl;
import com.csdn.dao.StudentDao;
import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
@Service
public class StudentServiceImpl2 implements StudentService {
@Autowired
StudentDao studentDao2;
public List<Student> findAll() throws SQLException {
return studentDao2.queryList();
}
}
Test.java
package com.csdn.test;
import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.SQLException;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring2.xml")
public class Test {
@Autowired
private StudentService studentService2;
@org.junit.Test
public void test01(){
List<Student> all = null;
try {
System.out.println(studentService2);
all = studentService2.findAll();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(all);
}
}