sprig 的配置文件 及 简单 test
1...pom.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.wsc.spring</groupId> 8 <artifactId>testSpring02</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <maven.compiler.source>1.8</maven.compiler.source> 15 <maven.compiler.target>1.8</maven.compiler.target> 16 </properties> 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-context</artifactId> 21 <version>5.0.2.RELEASE</version> 22 </dependency> 23 <dependency> 24 <groupId>commons-dbutils</groupId> 25 <artifactId>commons-dbutils</artifactId> 26 <version>1.6</version> 27 </dependency> 28 <dependency> 29 <groupId>log4j</groupId> 30 <artifactId>log4j</artifactId> 31 <version>1.2.17</version> 32 </dependency> 33 <dependency> 34 <groupId>org.mybatis</groupId> 35 <artifactId>mybatis</artifactId> 36 <version>3.4.5</version> 37 </dependency> 38 <dependency> 39 <groupId>junit</groupId> 40 <artifactId>junit</artifactId> 41 <version>4.12</version> 42 </dependency> 43 <dependency> 44 <groupId>c3p0</groupId> 45 <artifactId>c3p0</artifactId> 46 <version>0.9.1.2</version> 47 </dependency> 48 <dependency> 49 <groupId>mysql</groupId> 50 <artifactId>mysql-connector-java</artifactId> 51 <version>5.1.6</version> 52 </dependency> 53 </dependencies> 54 </project>
2...domain
1 package com.wsc.domain; 2 3 import java.util.Date; 4 5 /** 6 * @version 1.0 7 * @ClassName Student 8 * @Description TODO 9 * @Author WSC 10 * @Date 2019/7/3 11:27 11 **/ 12 public class Student { 13 private int studentNo; 14 private String studentName; 15 private int age; 16 private Date boreDate; 17 private int classNo; 18 19 public Student() { 20 super(); 21 } 22 23 public Student(int studentNo, String studentName, int age, Date boreDate, int classNo) { 24 super(); 25 this.studentNo = studentNo; 26 this.studentName = studentName; 27 this.age = age; 28 this.boreDate = boreDate; 29 this.classNo = classNo; 30 } 31 32 public int getStudentNo() { 33 return studentNo; 34 } 35 36 public void setStudentNo(int studentNo) { 37 this.studentNo = studentNo; 38 } 39 40 public String getStudentName() { 41 return studentName; 42 } 43 44 public void setStudentName(String studentName) { 45 this.studentName = studentName; 46 } 47 48 public int getAge() { 49 return age; 50 } 51 52 public void setAge(int age) { 53 this.age = age; 54 } 55 56 public Date getBoreDate() { 57 return boreDate; 58 } 59 60 public void setBoreDate(Date boreDate) { 61 this.boreDate = boreDate; 62 } 63 64 public int getClassNo() { 65 return classNo; 66 } 67 68 public void setClassNo(int classNo) { 69 this.classNo = classNo; 70 } 71 72 @Override 73 public String toString() { 74 return "Student [studentNo=" + studentNo + ", studentName=" + studentName + ", age=" + age + ", boreDate=" 75 + boreDate + ", classNo=" + classNo + "]"; 76 } 77 }
3...dao
1 package com.wsc.dao; 2 3 import com.wsc.domain.Student; 4 5 import java.util.List; 6 7 public interface StudentDao { 8 /** 9 * 查询所有结果 10 * @return 结果集合 11 */ 12 public List<Student> findAll(); 13 14 /** 15 * 通过id删除 16 * @param id 17 */ 18 public void drop(Integer id); 19 20 /** 21 * 通过id 查询 22 * @param id 23 */ 24 public Student selectId(Integer id); 25 26 /** 27 * 通过对象 添加数据 28 * @param student 29 */ 30 public void add(Student student); 31 32 /** 33 * 模糊查询 34 * @param name 35 */ 36 public List<Student> selectByName(String name); 37 38 /** 39 * 通过id 修改数据 40 * 41 */ 42 public void change(Student student); 43 }
4...daoImpl
1 package com.wsc.dao.impl; 2 3 import com.wsc.dao.StudentDao; 4 import com.wsc.domain.Student; 5 import org.apache.commons.dbutils.QueryRunner; 6 import org.apache.commons.dbutils.handlers.BeanHandler; 7 import org.apache.commons.dbutils.handlers.BeanListHandler; 8 9 import java.sql.SQLException; 10 import java.util.List; 11 12 /** 13 * @version 1.0 14 * @ClassName StudentDaoImpl 15 * @Description TODO 16 * @Author WSC 17 * @Date 2019/7/3 11:30 18 **/ 19 public class StudentDaoImpl implements StudentDao { 20 private QueryRunner queryRunner; 21 22 public void setQueryRunner(QueryRunner queryRunner) { 23 this.queryRunner = queryRunner; 24 } 25 26 @Override 27 public List<Student> findAll() { 28 String sql="select * from student"; 29 List<Student> query = null; 30 try { 31 query = queryRunner.query(sql, new BeanListHandler<Student>(Student.class)); 32 } catch (SQLException e) { 33 e.printStackTrace(); 34 } 35 return query; 36 } 37 38 @Override 39 public void drop(Integer id) { 40 String sql="delete from student where studentNo=? "; 41 try { 42 queryRunner.update(sql,id); 43 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 } 48 49 @Override 50 public Student selectId(Integer id){ 51 String sql="select * from student where studentNo=?"; 52 Student query=null; 53 try { 54 query = queryRunner.query(sql, new BeanHandler<Student>(Student.class), id); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 return query; 59 } 60 61 @Override 62 public void add(Student student) { 63 String sql="insert into student values(null,?,?,?,?)"; 64 try { 65 queryRunner.update(sql,student.getStudentName(),student.getAge(),student.getBoreDate(),student.getClassNo()); 66 } catch (SQLException e) { 67 e.printStackTrace(); 68 } 69 } 70 71 @Override 72 public List<Student> selectByName(String name) { 73 //select * from student where studentName like '%?%' 74 String sql="select * from student where studentName like '%?%'"; 75 List<Student> query=null; 76 try { 77 query = queryRunner.query(sql,new BeanListHandler<Student>(Student.class),name); 78 } catch (SQLException e) { 79 e.printStackTrace(); 80 } 81 return query; 82 } 83 84 @Override 85 public void change(Student student) { 86 String sql="update student set studentName=?,age=?,boreDate=?,classNo=? where StudentNo=?"; 87 try { 88 queryRunner.update(sql,student.getStudentName(),student.getAge(),student.getBoreDate(),student.getClassNo(),student.getStudentNo()); 89 } catch (SQLException e) { 90 e.printStackTrace(); 91 } 92 } 93 }
5....service
1 package com.wsc.service; 2 3 import com.wsc.domain.Student; 4 5 import java.util.List; 6 7 public interface StudentService { 8 /** 9 * 查询所有结果 10 * @return 结果集合 11 */ 12 public List<Student> findAll(); 13 14 /** 15 * 通过id删除 16 * @param id 17 */ 18 public void drop(Integer id); 19 20 /** 21 * 通过id 查询 22 * @param id 23 */ 24 public Student selectId(Integer id); 25 26 /** 27 * 通过对象 添加数据 28 * @param student 29 */ 30 public void add(Student student); 31 32 /** 33 * 模糊查询 34 * @param name 35 */ 36 public List<Student> selectByName(String name); 37 38 /** 39 * 通过id 修改数据 40 * 41 */ 42 public void change(Student student); 43 }
6...serviceImpl
1 package com.wsc.service.impl; 2 3 import com.wsc.dao.StudentDao; 4 import com.wsc.domain.Student; 5 import com.wsc.service.StudentService; 6 7 import java.util.List; 8 9 /** 10 * @version 1.0 11 * @ClassName StudentServiceImpl 12 * @Description TODO 13 * @Author WSC 14 * @Date 2019/7/3 11:40 15 **/ 16 public class StudentServiceImpl implements StudentService { 17 private StudentDao studentDao; 18 19 public void setStudentDao(StudentDao studentDao) { 20 this.studentDao = studentDao; 21 } 22 23 @Override 24 public List<Student> findAll() { 25 return studentDao.findAll(); 26 } 27 28 @Override 29 public void drop(Integer id) { 30 studentDao.drop(id); 31 } 32 33 @Override 34 public Student selectId(Integer id) { 35 return studentDao.selectId(id); 36 } 37 38 @Override 39 public void add(Student student) { 40 studentDao.add(student); 41 } 42 43 @Override 44 public List<Student> selectByName(String name) { 45 return studentDao.selectByName(name); 46 } 47 48 @Override 49 public void change(Student student) { 50 studentDao.change(student); 51 } 52 }
7...test
1 package com.wsc.testSpring; 2 3 import com.wsc.domain.Student; 4 import com.wsc.service.StudentService; 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import java.util.Date; 10 import java.util.List; 11 12 /** 13 * @version 1.0 14 * @ClassName testSpring02 15 * @Description TODO 16 * @Author WSC 17 * @Date 2019/7/3 14:15 18 **/ 19 public class testSpring02 { 20 ApplicationContext ac; 21 @Test 22 public void testFindAll(){ 23 ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 24 StudentService studentService = ac.getBean("studentService", StudentService.class); 25 List<Student> list = studentService.findAll(); 26 for(Student stu:list){ 27 System.out.println(stu); 28 } 29 } 30 @Test 31 public void testSelectById(){ 32 ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 33 StudentService studentService = ac.getBean("studentService", StudentService.class); 34 Student student = studentService.selectId(21); 35 System.out.println(student); 36 } 37 @Test 38 public void testAdd(){ 39 ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 40 StudentService studentService = ac.getBean("studentService", StudentService.class); 41 Student student = new Student(); 42 // student.setStudentNo(30); 43 student.setStudentName("张国"); 44 student.setAge(20); 45 Date date = new Date(); 46 student.setBoreDate(date); 47 student.setClassNo(1); 48 studentService.add(student); 49 } 50 @Test 51 public void change(){ 52 ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 53 StudentService studentService = ac.getBean("studentService", StudentService.class); 54 Student student = new Student(); 55 student.setStudentNo(21); 56 student.setStudentName("张国"); 57 student.setAge(20); 58 Date date = new Date(); 59 student.setBoreDate(date); 60 student.setClassNo(1); 61 studentService.change(student); 62 } 63 @Test 64 public void drop(){ 65 ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 66 StudentService studentService = ac.getBean("studentService", StudentService.class); 67 studentService.drop(14); 68 } 69 @Test 70 public void selectByName(){ 71 ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 72 StudentService studentService = ac.getBean("studentService", StudentService.class); 73 // Student student = new Student(); 74 // student.setStudentName("国"); 75 List<Student> list = studentService.selectByName("国"); 76 for(Student stu:list){ 77 System.out.println(stu); 78 } 79 } 80 }
8....xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <!--创建StudentDao对象 需要QueryRunner对象--> 11 <bean id="studentDao" class="com.wsc.dao.impl.StudentDaoImpl"> 12 <property name="queryRunner" ref="queryRunner"></property> 13 </bean> 14 <bean name="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> 15 <!--通过构造方法参数类型注入--> 16 <constructor-arg type="javax.sql.DataSource" ref="dateSource"></constructor-arg> 17 </bean> 18 <!--创建数据源 需要四个参数--> 19 <bean name="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 20 <!--通过set方法注入--> 21 <property name="driverClass" value="${jdbc.driver}"></property> 22 <property name="jdbcUrl" value="${jdbc.url}"></property> 23 <property name="user" value="${jdbc.name}"></property> 24 <property name="password" value="${jdbc.password}"></property> 25 </bean> 26 <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> 27 28 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <!--引入外部配置文件--> 8 <import resource="applicationContext-dao.xml"></import> 9 <!--创建studentService 对象 需要引入 studentDao对象 依赖注入 Dao 对象--> 10 <bean id="studentService" class="com.wsc.service.impl.StudentServiceImpl"> 11 <!--构造方法注入 set 方法注入--> 12 <property name="studentDao" ref="studentDao"></property> 13 </bean> 14 </beans>
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!wished for you successed !!!