本實驗是在使用maven工程控制版本的基礎上進行的,通過使用xml配置的方式來對mysql數據庫實現增刪查改功能。
1 配置環境
1.1 pom文件配置
這個文件裏配置了一個插件(由於我的IDEA的版本問題,override有些異常,導入這個插件可以解決,不用可以刪除)
同時有mysql驅動包,junit測試包,spring包,c3p0包,dbutils(一個封裝了jdbc功能的包,用起來很方便),以下是代碼。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 這裏不用可以刪除-->>
<groupId>indi.huangzi</groupId>
<artifactId>day1</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<!-- 這裏不用可以刪除-->>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
1.2 bean.xml文件的配置
我先把配置文件寫出來了,實際上你可以先寫實現類和接口類最後再寫Bean的配置類。(**我的這個註釋頭也可以用於註解的spring配置方式**) 內容如下:(其中我的數據庫database名字爲test1,其密碼爲233333)<?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="serviceImpl" class="Service.serviceImpl">
<property name="studentDao" ref="studentDAO"></property>
</bean>
<bean id="studentDAO" class="Service.DAO.DAO.daoImpl">
<property name="runner" ref="run"></property>
</bean>
<bean name="run" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<constructor-arg name="ds" ref="datasourse"></constructor-arg>
</bean>
<bean id="datasourse" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test1"></property>
<property name="user" value="root"></property>
<property name="password" value="233333"></property>
</bean>
</beans>
1.3 所有文件的擺放一覽圖
下面是所有的文件建立好之後的一覽圖,名字都可以自己確定。2 建立業務層和數據訪問層
2.1 業務層代碼
業務層代碼分爲接口類和實現類兩個,其中接口類取名爲Iservice,實現類取名爲serviceImpl
2.1.1 業務層接口代碼
package Service;
import model.student;
import java.util.List;
public interface Iservice {
/**
* 查詢所有
* @return
*/
List<student> findAllStudent();
/**
* 通過id查詢
* @return
*/
student findBystudentId(Integer id);
/**
* 保存學生實體
*/
void saveStudent(student s);
/**
* 修改學生實體
*/
void updateStudent(student s);
/**
* 通過id進行刪除
*/
void deleteById( Integer id);
}
2.1.2 業務層實現代碼
package Service;
import Service.DAO.Idao;
import model.student;
import java.util.List;
public class serviceImpl implements Iservice {
private Idao studentDao;
public void setStudentDao(Idao studentDao) {
this.studentDao = studentDao;
}
@Override
public List<student> findAllStudent() {
return studentDao.findAllStudent();
}
@Override
public student findBystudentId(Integer id) {
return studentDao.findBystudentId(id);
}
@Override
public void saveStudent(student s) {
studentDao.saveStudent(s);
}
@Override
public void updateStudent(student s) {
studentDao.updateStudent(s);
}
@Override
public void deleteById(Integer id) {
studentDao.deleteById(id);
}
}
2.2 數據訪問層代碼
2.2.1 數據層接口代碼
package Service.DAO;
import model.student;
import java.util.List;
public interface Idao {
List<student> findAllStudent();
/**
* 通過id查詢
* @return
*/
student findBystudentId(Integer id);
/**
* 通過id進行保存
*/
void saveStudent(student s);
/**
* 通過id進行修改
*/
void updateStudent(student s);
/**
* 通過id進行刪除
*/
void deleteById( Integer id);
}
2.2.2 數據層實現代碼
package Service.DAO.DAO;
import Service.DAO.Idao;
import model.student;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
public class daoImpl implements Idao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
@Override
//查找所有
public List<student> findAllStudent() {
try {
return runner.query("select *from student",new BeanListHandler<student>(student.class));
}catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
//按照Id查找
public student findBystudentId(Integer id) {
try {
return runner.query("select *from student where studentId=?",new BeanHandler<student>(student.class),id);
}catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
//保存實體
public void saveStudent(student s) {
try {
runner.update("insert into student(name,age)values(?,?)", s.getName(),s.getAge());
}catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
//更新
public void updateStudent(student s) {
try {
runner.update("update student set name=?,age=? where studentId =?", s.getName(),s.getAge(),s.getStudentId());
}catch (Exception e){
throw new RuntimeException(e);
}
}
@Override
//根據id刪除
public void deleteById(Integer id) {
try {
runner.update("delete from student where studentId =?", id);
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
2.3 model層(學生實體類)
這個類建立在model包下。記得創建一個model包。名字爲student,其屬性字段只有studentId,name,age這3個
package model;
import java.io.Serializable;
public class student implements Serializable {
private int studentId;
private String name;
private int age;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "student{" +
"studentId=" + studentId +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.4 測試類studentTest
test文件夾下的studentTest文件。
import Service.Iservice;
import model.student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class studentTest {
@Test
public void testFindAll(){
//獲取容器
ApplicationContext bean= new ClassPathXmlApplicationContext("bean.xml");
//獲取bean對象
Iservice service= bean.getBean("serviceImpl",Iservice.class);
//執行方法
List<student>students=service.findAllStudent();
//打印處數據庫裏的數據
for(student s : students){
System.out.println(s);
}
}
@Test
public void testFindById()
{
//獲取容器
ApplicationContext bean= new ClassPathXmlApplicationContext("bean.xml");
//獲取bean對象
Iservice service= bean.getBean("serviceImpl",Iservice.class);
//執行方法
student s=service.findBystudentId(2);
//打印處數據庫裏的數據
System.out.println(s);
}
@Test
public void update()
{
student s=new student();
s.setAge(24);
s.setName("qinyuan");
s.setStudentId(2);
//獲取容器
ApplicationContext bean= new ClassPathXmlApplicationContext("bean.xml");
//獲取bean對象
Iservice service= bean.getBean("serviceImpl",Iservice.class);
//執行方法
service.updateStudent(s);
//打印處數據庫裏的數據
}
@Test
public void save()
{
student s=new student();
s.setAge(388);
s.setName("qinshihuang");
//獲取容器
ApplicationContext bean= new ClassPathXmlApplicationContext("bean.xml");
//獲取bean對象
Iservice service= bean.getBean("serviceImpl",Iservice.class);
//執行方法
service.saveStudent(s);
//打印處數據庫裏的數據
}
@Test
public void delete()
{
//獲取容器
ApplicationContext bean= new ClassPathXmlApplicationContext("bean.xml");
//獲取bean對象
Iservice service= bean.getBean("serviceImpl",Iservice.class);
//執行方法
service.deleteById(4);
//打印處數據庫裏的數據
}
}
2.5 在數據庫內創建數據
打開mysql的命令行,然後輸入
第一步(創建數據庫test1):
create database test1;
第二步(使用數據庫test1)
use test1;
第三步(創建學生表)
create table student(
studentId int unsigned auto_increment,
name varchar(30) not null,
age int not null,
primary key (studentId))
;
第四步(插入數據若干)
insert into student(name,age) values('李曉明',20);
insert into student(name,age) values('王大錘',20);
insert into student(name,age) values('馬大雲',20);
之後就可以愉快的使用studentTest裏的方法啦!此處我就不再進行演示啦~