栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaWeb:(练习)十六、Spring学习练习二

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaWeb:(练习)十六、Spring学习练习二

JavaWeb:(练习)十六、Spring学习练习二

JavaWeb:(练习)十六、Spring学习练习二

一、学习目标二、dao包

1、BankDao2、StudentDao3、UserDao 三、model包

1、Bank2、Student3、User 四、service包

1、BankService2、StudentService3、UserService 五、Util工具包六、配置文件

1、pom.xml2、config.properties3、spring_backups1.xml(备份的一份spring配置文件)4、spring_backups2.xml(备份的一份spring配置文件)5、spring.xml 七、demo测试包

1、Demo12、Demo2

JavaWeb:(练习)十六、Spring学习练习二 一、学习目标

​ 初次接触Spring框架,学习了Spring的基础,基本了解了Spring的作用和优缺点。

​ 搭建了第一个Spring项目,导入Spring配置文件,学习并练习了IOC控制反转,并且进行Spring Bean管理,以xml配置方式和注解方式分别实现。并且练习了Spring JDBC。由此完成了spring练习一

​ JavaWeb:(练习)十五、Spring学习练习一

​ 在练习一的基础上,学习并练习了Spring AOP、Spring事务管理等。以下是在练习一的基础上,所改变的练习代码。

​ 注:关于UserDao改为接口,是因为在学习测试Spring集成MyBatis学习,其中包含错误。

二、dao包 1、BankDao
package com.ffyc.spring1.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository(value = "bankDao")
public class BankDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void addMoney() {
        jdbcTemplate.update("update bank set bank_money = bank_money + 500 where bank_account = 222");
    }

    public void reduceMoney() {
        jdbcTemplate.update("update bank set bank_money = bank_money - 500 where bank_account = 111");
    }
}

2、StudentDao
package com.ffyc.spring1.dao;

import com.ffyc.spring1.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository(value = "studentDao")
public class StudentDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void save() {

        

        List students = jdbcTemplate.query("select * from student where student_gender = ?", new RowMapper() {
            @Override
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                Student student = new Student();
                student.setStudentId(resultSet.getInt("student_id"));
                student.setStudentNumber(resultSet.getString("student_number"));
                student.setStudentName(resultSet.getString("student_name"));
                return student;
            }
        }, "男");
        System.out.println(students);
    }
}

3、UserDao
package com.ffyc.spring1.dao;

import com.ffyc.spring1.model.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository(value = "userDao")
public interface UserDao {
    

    public List findAllUsers();
}

三、model包 1、Bank
package com.ffyc.spring1.model;

import org.springframework.stereotype.Component;

@Component(value = "bank")
public class Bank {
    private Integer bankId;
    private String bankAccount;
    private Integer bankMoney;

    public Bank() {
    }

    public Bank(Integer bankId, String bankAccount, Integer bankMoney) {
        this.bankId = bankId;
        this.bankAccount = bankAccount;
        this.bankMoney = bankMoney;
    }

    public Integer getBankId() {
        return bankId;
    }

    public void setBankId(Integer bankId) {
        this.bankId = bankId;
    }

    public String getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(String bankAccount) {
        this.bankAccount = bankAccount;
    }

    public Integer getBankMoney() {
        return bankMoney;
    }

    public void setBankMoney(Integer bankMoney) {
        this.bankMoney = bankMoney;
    }

    @Override
    public String toString() {
        return "Bank{" +
                "bankId=" + bankId +
                ", bankAccount='" + bankAccount + ''' +
                ", bankMoney=" + bankMoney +
                '}';
    }
}

2、Student
package com.ffyc.spring1.model;

import org.springframework.stereotype.Component;

@Component(value = "student")
public class Student {
    private Integer studentId;
    private String studentNumber;
    private String studentName;

    public Student() {
    }

    public Student(Integer studentId, String studentNumber, String studentName) {
        this.studentId = studentId;
        this.studentNumber = studentNumber;
        this.studentName = studentName;
    }

    public Integer getStudentId() {
        return studentId;
    }

    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }

    public String getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentId=" + studentId +
                ", studentNumber='" + studentNumber + ''' +
                ", studentName='" + studentName + ''' +
                '}';
    }
}

3、User
package com.ffyc.spring1.model;

import org.springframework.stereotype.Component;

@Component(value = "user")
public class User {

    private Integer userId;
    private String userAccount;
    private String userPassword;

    public User() {
        System.out.println("无参");
    }

    public User(Integer userId, String userAccount, String userPassword) {
        System.out.println("有参");
        this.userId = userId;
        this.userAccount = userAccount;
        this.userPassword = userPassword;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(String userAccount) {
        this.userAccount = userAccount;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userAccount='" + userAccount + ''' +
                ", userPassword='" + userPassword + ''' +
                '}';
    }

}

四、service包 1、BankService
package com.ffyc.spring1.service;

import com.ffyc.spring1.dao.BankDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "bankService")
@Transactional
public class BankService {

    @Autowired
    BankDao bankDao;

    public void transMoney() {
        System.out.println("执行了转账操作!");
        bankDao.reduceMoney();
        bankDao.addMoney();
        System.out.println("转账操作执行完成!");
    }

    public BankService() {
    }

    public BankService(BankDao bankDao) {
        this.bankDao = bankDao;
    }

    public BankDao getBankDao() {
        return bankDao;
    }

    public void setBankDao(BankDao bankDao) {
        this.bankDao = bankDao;
    }
}

2、StudentService
package com.ffyc.spring1.service;

import com.ffyc.spring1.dao.StudentDao;
import com.ffyc.spring1.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.ws.soap.Addressing;

@Service(value = "studentService")
public class StudentService {
    

    

    

    @Resource(name = "studentDao")
    StudentDao studentDao;

    

    public void save() {
        System.out.println("StudentService.save()");
        System.out.println(10/0);
    }

    public void delete() {
        System.out.println("StudentService.delete()");
    }
}

3、UserService
package com.ffyc.spring1.service;

import com.ffyc.spring1.dao.UserDao;
import com.ffyc.spring1.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(value = "userService")
@Transactional
public class UserService {
    @Autowired
    UserDao userDao;



    public List findAllUsers() {
        List users = userDao.findAllUsers();
        return users;
    }

    public UserService() {
    }

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

五、Util工具包

MyBatis

package com.ffyc.spring1.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyUtil {

    @Before("execution(* com.ffyc.spring1.service.StudentService.save(..))")
    public void saveLog() {
        System.out.println("日志");
    }

    @After("execution(* com.ffyc.spring1.service.StudentService.save(..))")
    public void commit() {
        System.out.println("提交");
    }

    @Around("execution(* com.ffyc.spring1.service.StudentService.delete())")
    public void around(ProceedingJoinPoint proceedingJoinPoint) {
        System.out.println("=======前=======");
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("=======后=======");
    }

    @AfterThrowing(value = "execution(* com.ffyc.spring1.service.StudentService.save())", throwing = "e")
    public void execption(Throwable e) {
        System.out.println("异常通知:" + e.getMessage());
    }

}

六、配置文件 1、pom.xml
 
2、config.properties 
dirverClassName=com.mysql.cj.jdbc.Driver
url = jdbc:mysql://127.0.0.1:3306/ffycdb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
uname = root
password = root
3、spring_backups1.xml(备份的一份spring配置文件)


        
        
        

        
        
            
            
            
        

        
        

        
        
        
        
            
        

        


4、spring_backups2.xml(备份的一份spring配置文件)



    

    
    

    
    
    

    
    
        
        
        
        
        
            
            
            
            
            
            
            
            
        
    


5、spring.xml



    

    
    

    
    

    
    
        
    

    
    


七、demo测试包 1、Demo1
package com.ffyc.spring1.demo.day2;

import com.ffyc.spring1.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo2_1 {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
        StudentService studentService = app.getBean("studentService", StudentService.class);

        studentService.save();

        System.out.println("-------------------------------------");

        studentService.delete();
    }
}

2、Demo2
package com.ffyc.spring1.demo.day2;

import com.ffyc.spring1.service.BankService;
import com.ffyc.spring1.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo2_2 {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
        BankService bankService = app.getBean("bankService", BankService.class);

        bankService.transMoney();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/758968.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号