1. 案例结构
2. 所用数据库
3. 配置文件 jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/myschool?characterEncoding\=utf-8
username=root
password=root
initialSize=5
maxActive=50
maxIdle=10
minIdle=5
4. 配置文件 applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!-- 启用注解 -->
<context:annotation-config/>
<!--扫描生成单例实体对象 -->
<context:component-scan base-package="com.zhq.*"/>
<!-- 用于加载连接数据库的属性文件 -->
<bean id="preferencesPlaceholderConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location" value="classpath:config/jdbc.properties"/>
</bean>
<!--配置数据源,创建与数据库的连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<!--连接池配置 -->
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
</bean>
<!--采用Spring提供的Jdbc对象,可以操作数据 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--配置数据源对象 -->
<constructor-arg ref="dataSource"/>
</bean>
</beans>
5. pojo代码
package com.zhq.pojo;
import org.springframework.stereotype.Repository;
//配置文件扫描时生成实例Grade
@Repository
public class Grade {
private int gradeId;
private String gradeName;
public int getGradeId() {
return gradeId;
}
public void setGradeId(int gradeId) {
this.gradeId = gradeId;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
@Override
public String toString() {
return "Grade [gradeId=" + gradeId + ", gradeName=" + gradeName + "]";
}
}
6. dao 代码
package com.zhq.dao;
import java.util.List;
import com.zhq.pojo.Grade;
public interface GradeDao {
//查找所有的年级
public List<Grade> find();
//通过gradeId查找某个年级
public Grade findByGradeId(int id);
//添加一个新年级
public void add(Grade grade);
//通过gradeId删除某个年级
public void delete(int id);
//修改某个年级的信息
public void update(Grade grade);
}
package com.zhq.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.zhq.pojo.Grade;
//配置文件扫描时生成实例GradeDaoImpl_01
@Repository(value="GradeDaoImpl_01")
public class GradeDaoImpl_01 implements GradeDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
Grade grade;
@Override
public List<Grade> find() {
// TODO Auto-generated method stub
return null;
}
@Override
public Grade findByGradeId(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(Grade grade) {
// TODO Auto-generated method stub
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
@Override
public void update(Grade grade) {
// TODO Auto-generated method stub
}
}
package com.zhq.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.zhq.pojo.Grade;
//配置文件扫描时生成实例GradeDaoImpl_02
@Repository(value="GradeDaoImpl_02")
public class GradeDaoImpl_02 implements GradeDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
Grade grade;
@Override
//查询所有年级
public List<Grade> find() {
String sql="select * from grade";
//数据行与实体对象的映射
RowMapper<Grade> rm=new BeanPropertyRowMapper<>(Grade.class);
//query方法读取数据行,把数据映射成一个实体对象
List<Grade> list=jdbcTemplate.query(sql, rm);
return list;
}
@Override
public Grade findByGradeId(int id) {
String sql="select * from grade where gradeId=?";
//数据行与实体对象的映射
RowMapper<Grade> rm=new BeanPropertyRowMapper<>(Grade.class);
//queryForObject方法读取数据行,把数据映射成一个实体对象
Grade grade=jdbcTemplate.queryForObject(sql, new Object[] {id},rm);
return grade;
}
@Override
public void add(Grade grade) {
String sql="insert into grade values(null,?)";
jdbcTemplate.update(sql, grade.getGradeName());
}
@Override
public void delete(int id) {
String sql="delete from grade where gradeId=?";
jdbcTemplate.update(sql, id);
}
@Override
public void update(Grade grade) {
String sql="update grade set gradeName=? where gradeId=?";
jdbcTemplate.update(sql, grade.getGradeName(),grade.getGradeId());
}
}
7. service 代码
package com.zhq.service;
import java.util.List;
import com.zhq.pojo.Grade;
public interface GradeService {
//查询所有年级
public List<Grade> find();
//通过ID查找所有年级
public Grade findByGradeId(int id);
//添加一个新年级
public void add(Grade grade);
//通过gradeId删除某个年级
public void delete(int id);
//修改某个年级的信息
public void update(Grade grade);
}
package com.zhq.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zhq.dao.GradeDao;
import com.zhq.pojo.Grade;
//配置文件扫描时生成实例GradeServieImpl
@Service
public class GradeServieImpl implements GradeService {
//有两个GradeDao实例,表示要接收实例GradeDaoImpl_02
@Resource(name="GradeDaoImpl_02")
GradeDao dao;
@Override
public List<Grade> find() {
return dao.find();
}
@Override
public Grade findByGradeId(int id) {
return dao.findByGradeId(id);
}
@Override
public void add(Grade grade) {
dao.add(grade);
}
@Override
public void delete(int id) {
dao.delete(id);
}
@Override
public void update(Grade grade) {
dao.update(grade);
}
}
8. controller代码
package com.zhq.controller;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zhq.pojo.Grade;
import com.zhq.service.GradeService;
public class Main {
public static void main(String[] args) {
ApplicationContext cxt=new ClassPathXmlApplicationContext("config/applicationContext.xml");
GradeService ser=(GradeService) cxt.getBean(GradeService.class);
//获取所有的年级对象
List<Grade> list=ser.find();
if(list!=null) {
for(Grade grade:list) {
System.out.println(grade);
}
}
System.out.println("==================================");
//获取编号为18的班级
Grade grade=ser.findByGradeId(18);
if(grade!=null) {
System.out.println(grade);
}
//修改编号为18的班级信息
grade.setGradeName("八年级");
ser.update(grade);
//添加一个新年级
ser.add(grade);
//删除年级编号为22的班级
ser.delete(22);
}
}