概念
本文对Spring框架在项目的实际应用和搭建项目框架中的作用和应用的介绍。
1.搭建项目开发环境
本文连接的数据库是mydb,采用的表是account,银行账户信息的数据表,如果读者对该表的结构不太清楚,可查阅该文【MyBatis】第五课 银行账户管理系统网站开发
创建项目,将所需要的jar包导入lib文件夹中
按照MVC三层架构流程图创建controller层,service层,dao层以及util层,test层,分别对应于控制层,业务逻辑层,数据访问层,工具包和测试包等等。
对应的包名分别为:
com.account.controller, com.account.service,com.account.dao,
com.account.utils, com.account.test
2.创建每层对应的java类
在com.account.utils包中创建DBUtil类,用于采用JDBC连接mysql数据库,其代码如下:
package com.spring.utils;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import java.sql.Connection;import java.sql.DriverManager;/** * 连接数据库的工具类 */@Component(value = "db")public class DBUtil {@Value("com.mysql.cj.jdbc.Driver")private String driver;//连接数据库的驱动程序@Value("jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8")private String url;//访问数据库的地址@Value("root")private String username;//登录数据库的用户名@Value("admin")private String password;//登录数据库的密码public Connection connection=null;@PostConstructpublic void init(){//加载驱动程序try {Class.forName(driver);//连接数据库connection = DriverManager.getConnection(url, username, password);} catch (Exception e) {e.printStackTrace();}}}
在com.account.dao中创建数据访问的接口和实现类:
IAccountDao.java
package com.spring.dao;import java.util.List;import java.util.Map;/** * 数据访问层接口 */public interface IAccountDao {//将数据库中多条数据查询出来List<Map> getAll();}
AccountDaoImp.java
package com.spring.dao;import com.spring.utils.DBUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * 数据访问层实现类 */@Repository(value = "dao" )public class AccountDaoImp implements IAccountDao {@AutowiredDBUtil db;@Overridepublic List<Map> getAll() {List<Map> oList=new ArrayList();//如果connection对象不为空,说明连接数据库成功if (db.connection!=null){//执行sql语句String sql="select * from account";try {PreparedStatement ps=db.connection.prepareStatement(sql);ResultSet rs=ps.executeQuery();while (rs.next()){int id=rs.getInt("id");String password=rs.getString("password");String name=rs.getString("name");String personid=rs.getString("personid");double balance=rs.getDouble("balance");String opendate=rs.getString("opendate");Map oMap=new HashMap();oMap.put("id",id);oMap.put("password",password);oMap.put("name",name);oMap.put("personid",personid);oMap.put("balance",balance);oMap.put("opendate",opendate);oList.add(oMap);}} catch (SQLException e) {e.printStackTrace();}}return oList;}}
在com.account.service中创建业务逻辑层的接口和实现类:
IAccountService.java
package com.spring.service;import java.util.List;import java.util.Map;/** * 业务逻辑层接口 */public interface IAccountService {List<Map> getAll();}
AccountServiceImp.java
package com.spring.service;import com.spring.dao.IAccountDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;import java.util.Map;/** * 业务逻辑层接口实现类 */@Service(value = "service")public class AccountServiceImp implements IAccountService {@AutowiredIAccountDao dao;@Overridepublic List<Map> getAll() {return dao.getAll();}}
在com.account.controller中创建控制层的类:
AccountController.java
package com.spring.controller;import com.spring.service.IAccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import java.util.List;import java.util.Map;/** * 控制层 */@Controller(value = "controller")public class AccountController {@AutowiredIAccountService service;public List<Map> All(){return service.getAll();}}
在com.account.utils包中创建Spring框架的主配置文件类:
SpringUtil.java
package com.spring.utils;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("com.spring")public class SpringUtil {}
在com.account.test中创建测试类:
SpringTest.java
package com.spring.test;import com.spring.controller.AccountController;import com.spring.utils.SpringUtil;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class SpringTest {@Testpublic void one(){//先加载Spring框架的主配置文件ApplicationContext ac=new AnnotationConfigApplicationContext(SpringUtil.class);AccountController controller = ac.getBean("controller", AccountController.class);controller.All().forEach(map-> System.out.println(map));}}
总结
本文对Spring框架的实际的运行,希望对读者学习Spring框架有一定的帮助。