Spring
- 目的:解决企业应用开发的复杂性
- 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
- 范围:任何Java应用
Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。
增删改查
配置文件
<?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.zr"/>
<net.zr.bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/student?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</net.zr.bean>
<net.zr.bean id="jdbcTemlate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</net.zr.bean>
</beans>
实体类
通过自己的数据库设计
dao层
package com.zr.dao;
import com.zr.bean.Student;
import java.util.List;
public interface IStudentDao {
boolean addStu(Student student);
boolean delStu(int stuid);
boolean updStu(Student student);
Student findById(int stuid);
List<Student>findAll();
}
daoImpl
package com.zr.dao.impl;
import com.zr.bean.Student;
import com.zr.dao.IStudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class StudentDaoImpl implements IStudentDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public boolean addStu(Student student) {
boolean flag=false;
String sql="insert into user values(null,?,?,?,?)";
int row = jdbcTemplate.update(sql, student.getUsername(), student.getPassword(), student.getSex(), student.getAge());
if (row > 0) {
flag=true;
}
return flag;
}
@Override
public boolean delStu(int stuid) {
boolean flag=false;
String sql="delete from user where id=?";
int row = jdbcTemplate.update(sql,stuid);
if (row > 0) {
flag=true;
}
return flag;
}
@Override
public boolean updStu(Student student) {
boolean flag=false;
String sql="update user set username=?,password=?,sex=?,age=? where id=?";
int update = jdbcTemplate.update(sql, student.getUsername(), student.getPassword(), student.getSex(), student.getAge(), student.getId());
if (update>0) {
flag=true;
}
return flag;
}
@Override
public Student findById(int stuid) {
String sql="select * from user where id=?";
BeanPropertyRowMapper<Student> rowMapper=new BeanPropertyRowMapper<>(Student.class);
Student student=jdbcTemplate.queryForObject(sql,new Object[]{stuid},rowMapper);
return student;
}
@Override
public List<Student> findAll() {
String sql="select * from user";
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
List<Student> studentList = jdbcTemplate.query(sql, rowMapper);
return studentList;
}
}
service层
package com.zr.service;
import com.zr.bean.Student;
import java.util.List;
public interface IStudentService {
boolean addStu(Student student);
boolean delStu(int stuid);
boolean updStu(Student student);
Student findById(int stuid);
List<Student> findAll();
}
serviceImpl
package com.zr.service.impl;
import com.zr.bean.Student;
import com.zr.dao.IStudentDao;
import com.zr.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private IStudentDao studentDao;
@Override
public boolean addStu(Student student) {
return studentDao.addStu(student);
}
@Override
public boolean delStu(int stuid) {
return studentDao.delStu(stuid);
}
@Override
public boolean updStu(Student student) {
return studentDao.updStu(student);
}
@Override
public Student findById(int stuid) {
return studentDao.findById(stuid);
}
@Override
public List<Student> findAll() {
return studentDao.findAll();
}
}
测试类
package com.zr.test;
import com.zr.bean.Student;
import com.zr.service.impl.StudentServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Scanner;
public class TestStudent {
ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
static Scanner scanner=new Scanner(System.in);
public static void main(String[] args) {
while (true){
showMenu();
System.out.println("请输入你需要的功能");
int n=scanner.nextInt();
if (n==6){
System.out.println("已退出");
break;
}
switch (n){
case 1:
addStu();
break;
case 2:
delStu();
break;
case 3:
findById();
break;
case 4:
fingAll();
break;
case 5:
updateStu();
break;
default:
System.out.println("输入错误");
break;
}
}
}
private static void updateStu() {
ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
//创建对象
System.out.println("请输入姓名");
String username =scanner.next();
System.out.println("请输入密码");
String password=scanner.next();
System.out.println("请输入性别");
String sex= scanner.next();
System.out.println("请输入年龄");
int age=scanner.nextInt();
System.out.println("请输入你需要修改的ID号");
int id=scanner.nextInt();
Student student = new Student(username,password,sex,age,id);
//获取自动创建的对象-IOC
boolean b = ssi.updStu(student);
if (b){
System.out.println("修改成功");
}
else {
System.out.println("修改失败");
}
}
private static void fingAll() {
ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
StudentServiceImpl studentServiceImpl = (StudentServiceImpl) ac.getBean("studentServiceImpl");
List<Student> all = ssi.findAll();
for (Student stu:all){
System.out.println(
stu
);
}
}
private static void findById() {
ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
StudentServiceImpl studentServiceImpl = (StudentServiceImpl) ac.getBean("studentServiceImpl");
System.out.println("请输入ID号");
int id=scanner.nextInt();
Student byId = ssi.findById(id);
System.out.println(byId);
}
private static void delStu() {
ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
System.out.println("请输入需要删除的ID号");
int id=scanner.nextInt();
boolean b=ssi.delStu(id);
if (b){
System.out.println("删除成功");
}
else {
System.out.println("删除失败");
}
}
private static void addStu() {
ApplicationContext ac= new ClassPathXmlApplicationContext("/applicationContext.xml");
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
//创建对象
System.out.println("请输入姓名");
String username =scanner.next();
System.out.println("请输入密码");
String password=scanner.next();
System.out.println("请输入性别");
String sex= scanner.next();
System.out.println("请输入年龄");
int age=scanner.nextInt();
Student student = new Student(username,password,sex,age);
//获取自动创建的对象-IOC
boolean b = ssi.addStu(student);
if (b){
System.out.println("添加成功");
}
else {
System.out.println("添加失败");
}
}
private static void showMenu() {
System.out.println("欢迎使用屠吉吉管理的系统");
System.out.println("1,添加所有");
System.out.println("2,删除用户");
System.out.println("3,根据id查询用户");
System.out.println("4,查询所有用户");
System.out.println("5,修改用户");
System.out.println("6,退出");
}
}