├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── demo ├── DownloadServlet(简单的文件下载).java ├── JedisPool工具类 │ ├── jedis_utils.iml │ ├── src │ │ ├── cn │ │ │ └── mine │ │ │ │ └── jedis │ │ │ │ ├── test │ │ │ │ └── JedisTest.java │ │ │ │ └── utils │ │ │ │ └── JedisPoolUtils.java │ │ └── jedis.properties │ └── web │ │ └── index.jsp ├── mybatis文件配置 │ ├── mybatis_study_01 │ │ ├── mybatis_study_01.iml │ │ ├── pom.xml │ │ ├── src │ │ │ ├── main │ │ │ │ ├── java │ │ │ │ │ └── com │ │ │ │ │ │ └── mine │ │ │ │ │ │ ├── dao │ │ │ │ │ │ ├── UserMapper.java │ │ │ │ │ │ └── UserMapper.xml │ │ │ │ │ │ ├── pojo │ │ │ │ │ │ └── User.java │ │ │ │ │ │ └── utils │ │ │ │ │ │ └── MybatisUtils.java │ │ │ │ └── resources │ │ │ │ │ ├── db.properties │ │ │ │ │ └── mybatis_config.xml │ │ │ └── test │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── mine │ │ │ │ └── dao │ │ │ │ └── UserDaoTest.java │ │ └── target │ │ │ └── classes │ │ │ ├── com │ │ │ └── mine │ │ │ │ └── dao │ │ │ │ └── UserMapper.xml │ │ │ ├── db.properties │ │ │ └── mybatis_config.xml │ └── pom.xml ├── springMvc的基本使用、文件上传、拦截器 │ ├── pom.xml │ ├── spring_test.iml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── mine │ │ │ ├── controller │ │ │ └── HelloController.java │ │ │ ├── domain │ │ │ └── User.java │ │ │ ├── exception │ │ │ ├── SysException.java │ │ │ └── SysExceptionResolver.java │ │ │ └── interceptor │ │ │ └── MineInterceptor.java │ │ ├── resources │ │ └── springmvc.xml │ │ └── webapp │ │ ├── WEB-INF │ │ ├── pages │ │ │ ├── error.jsp │ │ │ ├── response.jsp │ │ │ └── success.jsp │ │ └── web.xml │ │ └── index.jsp ├── spring中jdbcTemplate的使用 │ ├── jdbc01.iml │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── mine │ │ │ ├── dao │ │ │ ├── IAccountDao.java │ │ │ ├── JdbcDaoSupport.java │ │ │ └── impl │ │ │ │ └── AccountImpl.java │ │ │ ├── domain │ │ │ └── Account.java │ │ │ └── jdbctemplate │ │ │ └── JdbcTemplateDemo1.java │ │ └── resources │ │ └── bean.xml ├── spring基于xml的Aop配置 │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mine │ │ │ │ ├── dao │ │ │ │ ├── IAccountDao.java │ │ │ │ └── impl │ │ │ │ │ └── AccountDaoImpl.java │ │ │ │ ├── domain │ │ │ │ └── Account.java │ │ │ │ ├── service │ │ │ │ ├── IAccountService.java │ │ │ │ └── impl │ │ │ │ │ └── AccountServiceImpl.java │ │ │ │ └── utils │ │ │ │ └── Logger.java │ │ └── resources │ │ │ └── bean.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── mine │ │ └── test │ │ └── AopTest.java ├── spring基于xml的Ioc配置 │ ├── demo01.iml │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mine │ │ │ │ ├── dao │ │ │ │ ├── IAccountDao.java │ │ │ │ └── impl │ │ │ │ │ └── AccountDaoImpl.java │ │ │ │ ├── domain │ │ │ │ └── Account.java │ │ │ │ └── service │ │ │ │ ├── IAccountService.java │ │ │ │ └── impl │ │ │ │ └── AccountServiceImpl.java │ │ └── resources │ │ │ └── bean.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── mine │ │ └── test.java ├── spring基于xml的声明式事务控制 │ ├── pom.xml │ ├── springTest.iml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── mine │ │ │ │ ├── dao │ │ │ │ ├── IAccountDao.java │ │ │ │ └── impl │ │ │ │ │ └── AccountImpl.java │ │ │ │ ├── domain │ │ │ │ └── Account.java │ │ │ │ └── service │ │ │ │ ├── IAccountService.java │ │ │ │ └── impl │ │ │ │ └── AccountServiceImpl.java │ │ └── resources │ │ │ └── bean.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── mine │ │ └── test │ │ └── SpringTest.java ├── ssm整合 │ ├── pom.xml │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── mine │ │ │ │ ├── controller │ │ │ │ └── AccountController.java │ │ │ │ ├── dao │ │ │ │ └── AccountDao.java │ │ │ │ ├── domain │ │ │ │ └── Account.java │ │ │ │ ├── service │ │ │ │ ├── AccountService.java │ │ │ │ └── impl │ │ │ │ │ └── AccountServiceImpl.java │ │ │ │ └── test │ │ │ │ ├── MybatisTest.java │ │ │ │ └── SpringTest.java │ │ │ ├── resources │ │ │ ├── applicationContext.xml │ │ │ ├── log4j.properties │ │ │ └── springmvc.xml │ │ │ └── webapp │ │ │ ├── WEB-INF │ │ │ ├── pages │ │ │ │ └── success.jsp │ │ │ └── web.xml │ │ │ └── index.jsp │ └── ssm.iml └── staff │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── mine │ │ │ ├── StaffApplication.java │ │ │ ├── config │ │ │ ├── LoginHandlerInterceptor.java │ │ │ ├── MyLocalResolver.java │ │ │ └── MyMvcConfig.java │ │ │ ├── controller │ │ │ ├── EmployeeController.java │ │ │ └── LoginController.java │ │ │ ├── dao │ │ │ ├── DepartmentDao.java │ │ │ └── EmployeeDao.java │ │ │ └── pojo │ │ │ ├── Department.java │ │ │ └── Employee.java │ └── resources │ │ ├── application.properties │ │ ├── i18n │ │ ├── login.properties │ │ ├── login_en_US.properties │ │ └── login_zh_CN.properties │ │ ├── static │ │ ├── css │ │ │ ├── bootstrap.min.css │ │ │ ├── dashboard.css │ │ │ └── signin.css │ │ ├── img │ │ │ └── bootstrap-solid.svg │ │ └── js │ │ │ ├── Chart.min.js │ │ │ ├── bootstrap.min.js │ │ │ ├── feather.min.js │ │ │ ├── jquery-3.2.1.slim.min.js │ │ │ └── popper.min.js │ │ └── templates │ │ ├── commons │ │ └── commons.html │ │ ├── dashboard.html │ │ ├── emp │ │ ├── add.html │ │ ├── list.html │ │ └── update.html │ │ ├── error │ │ └── 404.html │ │ └── index.html │ └── test │ └── java │ └── com │ └── mine │ └── StaffApplicationTests.java └── note ├── SSM的整合.md ├── Spring基于xml的Ioc实现.md ├── Spring基于注解的Ioc实现.md ├── mybatis使用教程.md ├── mysql系列之从入门到删库.md ├── springBoot工程部署与集成.md ├── springMvc使用教程.md ├── spring基于xml和注解的AOP实现.md └── spring系统中常用的注解梳理.md /.gitattributes: -------------------------------------------------------------------------------- 1 | *.js linguist-language=java 2 | *.css linguist-language=java 3 | *.html linguist-language=java 4 | *.md linguist-language=java 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | *.DS_Store 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 记录java的一些基础、框架学习笔记、 知识点梳理. 2 | 3 | ### java基础 4 | - [mysql基础入门全家桶](note/mysql入门全家桶.md) 5 | - [springMvc的基本使用、自定义异常处理、文件上传、拦截器等](note/springMvc使用教程.md) 6 | - [SSM的整合指南](note/SSM的整合.md) 7 | - [springBoot部署工程及集成mybatis、junit、yml](note/springBoot部署工程及集成mybatis、junit、yml.md) 8 | - [spring基于xml和注解Aop实现](note/spring基于xml和注解的AOP实现.md) 9 | - [spring基于注解的Ioc实现](note/Spring基于注解的Ioc实现.md) 10 | - [spring基于xml的Ioc实现](note/Spring基于xml的Ioc实现.md) 11 | - [mybatis使用教程](note/mybatis使用教程.md) 12 | 13 | 14 | ### 实例Demo 15 | 16 | 17 | - [springMvc的基本使用、文件上传、拦截器](demo/springMvc的基本使用、文件上传、拦截器) 18 | - [spring基于xml的声明式事务控制](demo/spring基于xml的声明式事务控制) 19 | - [mybatis文件配置](demo/mybatis文件配置) 20 | - [Spring基于xml的Ioc实现](demo/spring基于xml的Ioc配置) 21 | - [spring中jdbcTemplate的使用](demo/spring中jdbcTemplate的使用) 22 | -------------------------------------------------------------------------------- /demo/DownloadServlet(简单的文件下载).java: -------------------------------------------------------------------------------- 1 | package cn.itcast.web.download; 2 | 3 | import cn.itcast.web.utils.DownLoadUtils; 4 | import javax.servlet.ServletContext; 5 | import javax.servlet.ServletException; 6 | import javax.servlet.ServletOutputStream; 7 | import javax.servlet.annotation.WebServlet; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | import java.io.FileInputStream; 12 | import java.io.IOException; 13 | 14 | @WebServlet("/downloadServlet") 15 | public class DownloadServlet extends HttpServlet { 16 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 | // 1.获取请求参数,文件名称 18 | String filename = request.getParameter("filename"); 19 | // 2.使用字节输入流加载文件进内存 20 | // 2.1找到文件服务器路径 21 | ServletContext servletContext = this.getServletContext(); 22 | String realPath = servletContext.getRealPath("/img/" + filename); 23 | // 2.2用字节流关联 24 | FileInputStream fis = new FileInputStream(realPath); 25 | // 3.设置response的响应头 26 | // 3.1设置响应头类型:content-type 27 | String mimeType = servletContext.getMimeType(filename); //获取文件的mime类型 28 | response.setHeader("content-type", mimeType); 29 | // 3.2设置响应头打开方式:content-disposition 30 | // 解决中文文件名问题 31 | // 1.获取user-agent请求头、 32 | String agent = request.getHeader("user-agent"); 33 | // 2.使用工具类方法编码文件名即可 34 | filename = DownLoadUtils.getFileName(agent, filename); 35 | response.setHeader("content-disposition","attachment;filename="+filename); 36 | //4.将输入流的数据写出到输出流中 37 | ServletOutputStream sos = response.getOutputStream(); 38 | byte[] buff = new byte[1024 * 8]; 39 | int len = 0; 40 | while((len = fis.read(buff)) != -1){ 41 | sos.write(buff, 0, len); 42 | } 43 | fis.close(); 44 | } 45 | 46 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 47 | this.doPost(request,response); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /demo/JedisPool工具类/jedis_utils.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /demo/JedisPool工具类/src/cn/mine/jedis/test/JedisTest.java: -------------------------------------------------------------------------------- 1 | package cn.mine.jedis.test; 2 | 3 | import cn.mine.jedis.utils.JedisPoolUtils; 4 | import redis.clients.jedis.Jedis; 5 | 6 | public class JedisTest { 7 | public void test() { 8 | Jedis jedis = JedisPoolUtils.getJedis(); 9 | jedis.set("hello","world"); 10 | jedis.close(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /demo/JedisPool工具类/src/cn/mine/jedis/utils/JedisPoolUtils.java: -------------------------------------------------------------------------------- 1 | package cn.mine.jedis.utils; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisPool; 5 | import redis.clients.jedis.JedisPoolConfig; 6 | 7 | import java.io.InputStream; 8 | import java.util.Properties; 9 | 10 | /** 11 | * JedisPool工具类 12 | * 加载配置文件, 配置连接池参数 13 | * 提供获取连接的方法 14 | */ 15 | public class JedisPoolUtils { 16 | private static JedisPool jedisPool; 17 | static { 18 | // 读取配置文件 19 | InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties"); 20 | Properties pro = new Properties(); 21 | 22 | // 获取数据,设置到jedisPoolConfig中 23 | JedisPoolConfig config = new JedisPoolConfig(); 24 | config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal"))); 25 | config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle"))); 26 | 27 | // 初始化jedisPool 28 | jedisPool = new JedisPool(config,pro.getProperty("host"), Integer.parseInt(pro.getProperty("port"))); 29 | } 30 | 31 | /** 32 | * 获取连接方法 33 | */ 34 | public static Jedis getJedis() { 35 | return jedisPool.getResource(); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /demo/JedisPool工具类/src/jedis.properties: -------------------------------------------------------------------------------- 1 | host=127.0.0.1 2 | port=6379 3 | maxTotal=50 4 | maxIdle=10 -------------------------------------------------------------------------------- /demo/JedisPool工具类/web/index.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: chentao 4 | Date: 2019/10/14 5 | Time: 8:45 上午 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | $Title$ 12 | 13 | 14 | $END$ 15 | 16 | 17 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/mybatis_study_01.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | mybatis_01 7 | cn.mineTest 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | mybatis_study_01 13 | 14 | 15 | 16 | 17 | src/main/resources 18 | 19 | **/*.properties 20 | **/*.xml 21 | 22 | true 23 | 24 | 25 | src/main/java 26 | 27 | **/*.properties 28 | **/*.xml 29 | 30 | true 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/src/main/java/com/mine/dao/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.pojo.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | List getUserList(); 9 | User getUserById(int id); 10 | int addUser(User user); 11 | int updateUser(User user); 12 | int deleteUser(int id); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/src/main/java/com/mine/dao/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 15 | 16 | 17 | insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd}) 18 | 19 | 20 | 21 | update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id} 22 | 23 | 24 | 25 | delete from mybatis.user where id = #{id} 26 | 27 | 28 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/src/main/java/com/mine/pojo/User.java: -------------------------------------------------------------------------------- 1 | package com.mine.pojo; 2 | 3 | import org.apache.ibatis.type.Alias; 4 | 5 | public class User { 6 | private int id; 7 | private String name; 8 | private String pwd; 9 | 10 | public User() { 11 | } 12 | 13 | public User(int id, String name, String pwd) { 14 | this.id = id; 15 | this.name = name; 16 | this.pwd = pwd; 17 | } 18 | 19 | public int getId() { 20 | return id; 21 | } 22 | 23 | public void setId(int id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getPwd() { 36 | return pwd; 37 | } 38 | 39 | public void setPwd(String pwd) { 40 | this.pwd = pwd; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "User{" + 46 | "id=" + id + 47 | ", name='" + name + '\'' + 48 | ", pwd='" + pwd + '\'' + 49 | '}'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/src/main/java/com/mine/utils/MybatisUtils.java: -------------------------------------------------------------------------------- 1 | package com.mine.utils; 2 | 3 | import org.apache.ibatis.io.Resources; 4 | import org.apache.ibatis.session.SqlSession; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | // sqlSessionFactory工具类 11 | public class MybatisUtils { 12 | private static SqlSessionFactory sqlSessionFactory; 13 | static { 14 | try { 15 | String resource = "mybatis_config.xml"; 16 | InputStream inputStream = Resources.getResourceAsStream(resource); 17 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 18 | } catch (IOException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | 23 | // 获取可以执行sql的sqlSession对象 24 | public static SqlSession getSqlSession() { 25 | return sqlSessionFactory.openSession(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/src/main/resources/db.properties: -------------------------------------------------------------------------------- 1 | driver=com.mysql.jdbc.Driver 2 | url=jdbc:mysql://localhost:3306/mybatis?useSSL=false 3 | username=root 4 | password=12345678 -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/src/main/resources/mybatis_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/src/test/java/com/mine/dao/UserDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.pojo.User; 4 | import com.mine.utils.MybatisUtils; 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.junit.Test; 7 | 8 | import java.util.List; 9 | 10 | public class UserDaoTest { 11 | @Test 12 | public void test() { 13 | SqlSession sqls = MybatisUtils.getSqlSession(); 14 | UserMapper userDao = sqls.getMapper(UserMapper.class); 15 | List userList = userDao.getUserList(); 16 | for (User user : userList) { 17 | System.out.println(user); 18 | } 19 | sqls.close(); 20 | } 21 | 22 | @Test 23 | public void getUserById() { 24 | SqlSession sqlSession = MybatisUtils.getSqlSession(); 25 | UserMapper mapper = sqlSession.getMapper(UserMapper.class); 26 | User user = mapper.getUserById(3); 27 | System.out.println(user); 28 | } 29 | 30 | @Test 31 | public void addUser() { 32 | SqlSession sqlSession = MybatisUtils.getSqlSession(); 33 | UserMapper mapper = sqlSession.getMapper(UserMapper.class); 34 | int res =mapper.addUser(new User(5,"张oo","123122 ")); 35 | if (res>0) { 36 | System.out.println("插入成功"); 37 | } 38 | sqlSession.commit(); 39 | sqlSession.close(); 40 | } 41 | 42 | @Test 43 | public void updateUser() { 44 | SqlSession sqlSession = MybatisUtils.getSqlSession(); 45 | UserMapper mapper = sqlSession.getMapper(UserMapper.class); 46 | mapper.updateUser(new User(4,"王二小","67744444")); 47 | sqlSession.commit(); 48 | sqlSession.close(); 49 | } 50 | 51 | @Test 52 | public void deleteUser() { 53 | SqlSession sqlSession = MybatisUtils.getSqlSession(); 54 | UserMapper mapper = sqlSession.getMapper(UserMapper.class); 55 | mapper.deleteUser(1); 56 | sqlSession.commit(); 57 | sqlSession.close(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/target/classes/com/mine/dao/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 15 | 16 | 17 | insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd}) 18 | 19 | 20 | 21 | update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id} 22 | 23 | 24 | 25 | delete from mybatis.user where id = #{id} 26 | 27 | 28 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/target/classes/db.properties: -------------------------------------------------------------------------------- 1 | driver=com.mysql.jdbc.Driver 2 | url=jdbc:mysql://localhost:3306/mybatis?useSSL=false 3 | username=root 4 | password=12345678 -------------------------------------------------------------------------------- /demo/mybatis文件配置/mybatis_study_01/target/classes/mybatis_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /demo/mybatis文件配置/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | cn.mineTest 9 | mybatis_01 10 | pom 11 | 1.0-SNAPSHOT 12 | 13 | mybatis_study_01 14 | 15 | 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.46 21 | 22 | 23 | junit 24 | junit 25 | 4.12 26 | 27 | 28 | org.mybatis 29 | mybatis 30 | 3.5.2 31 | 32 | 33 | 34 | 35 | 36 | src/main/resources 37 | 38 | **/*.properties 39 | **/*.xml 40 | 41 | true 42 | 43 | 44 | src/main/java 45 | 46 | **/*.properties 47 | **/*.xml 48 | 49 | true 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.mine 7 | spring_test 8 | 1.0-SNAPSHOT 9 | war 10 | 11 | spring_test Maven Webapp 12 | 13 | http://www.example.com 14 | 15 | 16 | UTF-8 17 | 1.8 18 | 1.8 19 | 5.0.2.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework 25 | spring-context 26 | ${spring.version} 27 | 28 | 29 | org.springframework 30 | spring-web 31 | ${spring.version} 32 | 33 | 34 | org.springframework 35 | spring-webmvc 36 | ${spring.version} 37 | 38 | 39 | javax.servlet 40 | servlet-api 41 | 2.5 42 | provided 43 | 44 | 45 | javax.servlet.jsp 46 | jsp-api 47 | 2.0 48 | provided 49 | 50 | 51 | commons-fileupload 52 | commons-fileupload 53 | 1.3.1 54 | 55 | 56 | commons-io 57 | commons-io 58 | 2.4 59 | 60 | 61 | com.sun.jersey 62 | jersey-core 63 | 1.18.1 64 | 65 | 66 | com.sun.jersey 67 | jersey-client 68 | 1.18.1 69 | 70 | 71 | junit 72 | junit 73 | 4.12 74 | test 75 | 76 | 77 | 78 | 79 | spring_test 80 | 81 | 82 | 83 | maven-clean-plugin 84 | 3.1.0 85 | 86 | 87 | 88 | maven-resources-plugin 89 | 3.0.2 90 | 91 | 92 | maven-compiler-plugin 93 | 3.8.0 94 | 95 | 96 | maven-surefire-plugin 97 | 2.22.1 98 | 99 | 100 | maven-war-plugin 101 | 3.2.2 102 | 103 | 104 | maven-install-plugin 105 | 2.5.2 106 | 107 | 108 | maven-deploy-plugin 109 | 2.8.2 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/spring_test.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/java/com/mine/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.mine.controller; 2 | 3 | import com.mine.domain.User; 4 | import com.mine.exception.SysException; 5 | import com.sun.jersey.api.client.Client; 6 | import com.sun.jersey.api.client.WebResource; 7 | import org.apache.commons.fileupload.FileItem; 8 | import org.apache.commons.fileupload.FileUploadException; 9 | import org.apache.commons.fileupload.disk.DiskFileItemFactory; 10 | import org.apache.commons.fileupload.servlet.ServletFileUpload; 11 | import org.springframework.http.HttpRequest; 12 | import org.springframework.stereotype.Controller; 13 | import org.springframework.ui.Model; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.multipart.MultipartFile; 16 | 17 | import javax.servlet.ServletException; 18 | import javax.servlet.http.HttpServletRequest; 19 | import javax.servlet.http.HttpServletResponse; 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.util.List; 23 | import java.util.UUID; 24 | 25 | @Controller 26 | @RequestMapping("/hello") 27 | public class HelloController { 28 | @RequestMapping("/sayHello") 29 | public String sayHello(Model model) { 30 | System.out.println("访问了hello方法"); 31 | User user = new User(); 32 | user.setUsername("李白"); 33 | user.setPassword("234"); 34 | user.setAge(15); 35 | model.addAttribute("user",user); 36 | return "success"; 37 | } 38 | 39 | @RequestMapping("/getUser") 40 | public void getUser(HttpServletRequest request, HttpServletResponse response) throws Exception { 41 | System.out.println("getuser方法执行了"); 42 | // 手动转发需要完整路径 43 | // request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response); 44 | // 重定向 45 | // response.sendRedirect(request.getContextPath() + "/index.jsp"); 46 | // 直接响应数据 47 | response.setCharacterEncoding("UTF-8"); 48 | response.setContentType("text/html;charset=UTF-8"); 49 | response.getWriter().write("你好"); 50 | return; 51 | } 52 | 53 | //传统的上传方式 54 | @RequestMapping("/upload") 55 | public String uploadFile(HttpServletRequest request) throws Exception { 56 | System.out.println("文件上传.."); 57 | // 获取到需要上传文件的路径 58 | String realPath = request.getSession().getServletContext().getRealPath("/uploads/"); 59 | // 获取file路径,向路径上传文件 60 | File file = new File(realPath); 61 | // 判断路径是否存在 62 | if (!file.exists()) { 63 | file.mkdirs(); 64 | } 65 | // 创建磁盘文件工厂方法 66 | DiskFileItemFactory factory = new DiskFileItemFactory(); 67 | ServletFileUpload fileUpload = new ServletFileUpload(factory); 68 | // 解析request对象 69 | List list = fileUpload.parseRequest(request); 70 | // 遍历 71 | for (FileItem item : list) { 72 | // 判断是普通字段还是文件上传 73 | if (item.isFormField()) { 74 | } else { 75 | // 获取到上传文件的名称 76 | String fileName = item.getName(); 77 | String uuid = UUID.randomUUID().toString().replace("-",""); 78 | fileName = uuid + "_" + fileName; 79 | // 上传文件 80 | item.write(new File(file, fileName)); 81 | // 删除临时文件 82 | item.delete(); 83 | } 84 | } 85 | return "success"; 86 | } 87 | 88 | @RequestMapping("/uploadFileBySpring") 89 | // 要求MultipartFile对象变量名称必须和表单file标签的name属性名称相同。 90 | public String uploadFileBySpring(HttpServletRequest request, MultipartFile upload) throws Exception { 91 | System.out.println("springmvc的文件上传"); 92 | String path = request.getSession().getServletContext().getRealPath("/mvcUpload"); 93 | File file = new File(path); 94 | if (!file.exists()) { 95 | file.mkdirs(); 96 | } 97 | // 通过upload获取文件的名字 98 | String filename = upload.getOriginalFilename(); 99 | // 设置文件名为唯一值,拼接uuid 100 | String uuid = UUID.randomUUID().toString().replace("-", ""); 101 | filename = uuid + "_" + filename; 102 | upload.transferTo(new File(path,filename)); 103 | return "success"; 104 | } 105 | 106 | @RequestMapping("/uploadOtherServlet") 107 | public String uploadOtherServlet(HttpServletRequest request, MultipartFile upload) throws Exception { 108 | // 获取上传的文件的路径 109 | String path = "http://localhost:8080/uploads/"; 110 | String filename = upload.getOriginalFilename(); 111 | String uuid = UUID.randomUUID().toString().replace("-", ""); 112 | filename = uuid + "_" + filename; 113 | // 创建客户端的连接 114 | Client client = Client.create(); 115 | // 和图片服务器进行连接 116 | WebResource webResource = client.resource(path + filename); 117 | // 上传 118 | webResource.put(upload.getBytes()); 119 | return "success"; 120 | } 121 | // 异常处理类 122 | @RequestMapping("/exceptionTest") 123 | public String exceptionTest() throws SysException { 124 | System.out.println("异常处理测试"); 125 | try { 126 | int i = 110 / 0; 127 | } catch (Exception e) { 128 | // 打印异常信息 129 | e.printStackTrace(); 130 | throw new SysException("出现异常了,自定义异常友好界面抛出"); 131 | } 132 | return "success"; 133 | } 134 | 135 | // 拦截器方法 136 | @RequestMapping("testInterceptor") 137 | public String testInterceptor() { 138 | System.out.println("拦截器方法"); 139 | return "success"; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/java/com/mine/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.mine.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | private String username; 7 | private String password; 8 | private Integer age; 9 | 10 | public String getUsername() { 11 | return username; 12 | } 13 | 14 | public void setUsername(String username) { 15 | this.username = username; 16 | } 17 | 18 | public String getPassword() { 19 | return password; 20 | } 21 | 22 | public void setPassword(String password) { 23 | this.password = password; 24 | } 25 | 26 | public Integer getAge() { 27 | return age; 28 | } 29 | 30 | public void setAge(Integer age) { 31 | this.age = age; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/java/com/mine/exception/SysException.java: -------------------------------------------------------------------------------- 1 | package com.mine.exception; 2 | 3 | // 自定义异常类 4 | public class SysException extends Exception { 5 | // 存储提示信息 6 | private String message; 7 | @Override 8 | public String getMessage() { 9 | return message; 10 | } 11 | public void setMessage(String message) { 12 | this.message = message; 13 | } 14 | public SysException(String message) { 15 | this.message = message; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/java/com/mine/exception/SysExceptionResolver.java: -------------------------------------------------------------------------------- 1 | package com.mine.exception; 2 | 3 | import org.springframework.web.servlet.HandlerExceptionResolver; 4 | import org.springframework.web.servlet.ModelAndView; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | // 异常处理器 10 | public class SysExceptionResolver implements HandlerExceptionResolver { 11 | 12 | @Override 13 | public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { 14 | // 获取异常信息 15 | SysException exception = null; 16 | if (e instanceof SysException) { 17 | exception = (SysException) e; 18 | } else { 19 | exception = new SysException("系统正在升级, 请于...在来访问"); 20 | } 21 | ModelAndView mv = new ModelAndView(); 22 | mv.addObject("errorMsg",exception.getMessage()); 23 | mv.setViewName("error"); // 指定跳转的页面 24 | return mv; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/java/com/mine/interceptor/MineInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.mine.interceptor; 2 | 3 | import org.springframework.web.servlet.HandlerInterceptor; 4 | import org.springframework.web.servlet.ModelAndView; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | // 自定义拦截器,需要实现HandlerInterceptor接口 9 | public class MineInterceptor implements HandlerInterceptor { 10 | @Override 11 | // 预处理, controller执行之前 12 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 13 | // 返回ture表示放行,不拦截 14 | // 返回false 表示拦截 15 | System.out.println("preInterceptor执行了----之前"); 16 | // request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response); 17 | return true; 18 | } 19 | 20 | @Override 21 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 22 | System.out.println("interceptor执行---之后"); 23 | // 指定页面后跳转至指定的页面, 而不会再跳控制器中指定的页面了 24 | request.getRequestDispatcher("/WEB-INF/pages/response.jsp").forward(request,response); 25 | } 26 | 27 | @Override 28 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 29 | System.out.println("interceptor----after"); 30 | // 不能再跳转了 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/resources/springmvc.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/webapp/WEB-INF/pages/error.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: chentao 4 | Date: 2019/11/5 5 | Time: 11:00 上午 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> 9 | 10 | 11 | Title 12 | 13 | 14 |

拦截器测试

15 | 16 | 17 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/webapp/WEB-INF/pages/response.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: chentao 4 | Date: 2019/11/4 5 | Time: 8:13 下午 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | Title 12 | 13 | 14 |

拦截测试2

15 | 16 | 17 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/webapp/WEB-INF/pages/success.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: chentao 4 | Date: 2019/11/4 5 | Time: 6:14 下午 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> 9 | 10 | 11 | Title 12 | 13 | 14 | 15 |

欢迎异常测试类

16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | 9 | dispatcherServlet 10 | org.springframework.web.servlet.DispatcherServlet 11 | 12 | contextConfigLocation 13 | classpath:springmvc.xml 14 | 15 | 1 16 | 17 | 18 | dispatcherServlet 19 | / 20 | 21 | 22 | -------------------------------------------------------------------------------- /demo/springMvc的基本使用、文件上传、拦截器/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: chentao 4 | Date: 2019/11/4 5 | Time: 6:11 下午 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | 欢迎光临 12 | 13 | 14 |

传统的文件上传方式测试

15 |
16 | 选择文件:
17 | 18 |
19 |
20 |

springMVC的文件上传测试

21 |
22 | 选择文件:
23 | 24 |
25 |
26 | 异常处理类测试 27 |
28 | 拦截器测试 29 | 30 | 31 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/jdbc01.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | jdbc01 8 | jdbc01 9 | 1.0-SNAPSHOT 10 | jar 11 | 12 | 13 | 14 | 15 | org.springframework 16 | spring-context 17 | 5.1.5.RELEASE 18 | 19 | 20 | org.springframework 21 | spring-jdbc 22 | 5.1.5.RELEASE 23 | 24 | 25 | 26 | org.springframework 27 | spring-tx 28 | 5.1.5.RELEASE 29 | 30 | 31 | 32 | mysql 33 | mysql-connector-java 34 | 8.0.13 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/src/main/java/com/mine/dao/IAccountDao.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.domain.Account; 4 | 5 | public interface IAccountDao { 6 | 7 | Account getAccountById(Integer accountId); 8 | 9 | Account getAccountByName(String accountName); 10 | 11 | void updateAccount(Account account); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/src/main/java/com/mine/dao/JdbcDaoSupport.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import org.springframework.jdbc.core.JdbcTemplate; 4 | 5 | import javax.sql.DataSource; 6 | 7 | /** 8 | * 用于处理多个dao中的重复代码. 9 | * 当前模块本身在spring中已经实现过, 当JdbcDaoSupport继承spring的时候, 10 | * 不需要我们在自定义这个类来处理重复代码的情况. 11 | * 但是由于jdbcTemplate在sping的jar包内部,所以如果是基于纯注解开发,则无法注入jdbcTemplate. 12 | * 所以在纯注解开发中,我们一般采用当前文件中的方式自定义JdbcDaoSupport来抽取代码的重复部分 13 | * 当我们使用xml配置时, 可以直接继承spring的JdbcDaoSupport即可,不需要在提供JdbcDaoSupport类来处理. 14 | */ 15 | public class JdbcDaoSupport { 16 | private JdbcTemplate jdbcTemplate; 17 | private DataSource dataSource; 18 | 19 | public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 20 | this.jdbcTemplate = jdbcTemplate; 21 | } 22 | 23 | public JdbcTemplate getJdbcTemplate() { 24 | return jdbcTemplate; 25 | } 26 | 27 | public void setDataSource(DataSource dataSource) { 28 | this.dataSource = dataSource; 29 | if (jdbcTemplate == null) { 30 | jdbcTemplate = createJdbcTemplate(dataSource); 31 | } 32 | } 33 | 34 | private JdbcTemplate createJdbcTemplate(DataSource dataSource) { 35 | return new JdbcTemplate(dataSource); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/src/main/java/com/mine/dao/impl/AccountImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao.impl; 2 | 3 | import com.mine.dao.IAccountDao; 4 | import com.mine.domain.Account; 5 | import org.springframework.jdbc.core.BeanPropertyRowMapper; 6 | import org.springframework.jdbc.core.support.JdbcDaoSupport; 7 | 8 | import java.util.List; 9 | 10 | public class AccountImpl extends JdbcDaoSupport implements IAccountDao { 11 | public Account getAccountById(Integer accountId) { 12 | List accounts = getJdbcTemplate().query("select * from count where id = ?", new BeanPropertyRowMapper(Account.class),accountId); 13 | return accounts.isEmpty() ? null : accounts.get(0); 14 | } 15 | 16 | public Account getAccountByName(String accountName) { 17 | List accounts = getJdbcTemplate().query("select * from count where name = ?", new BeanPropertyRowMapper(Account.class),accountName); 18 | if (accounts.isEmpty()) { 19 | return null; 20 | } 21 | if (accounts.size() > 1) { 22 | throw new RuntimeException("查询结果不唯一"); 23 | } 24 | return accounts.get(0); 25 | } 26 | 27 | public void updateAccount(Account account) { 28 | getJdbcTemplate().update("update count set name = ?, money = ? where id = ?",account.getName(),account.getMoney(),account.getId()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/src/main/java/com/mine/domain/Account.java: -------------------------------------------------------------------------------- 1 | package com.mine.domain; 2 | 3 | import org.springframework.jdbc.core.ResultSetExtractor; 4 | 5 | import java.io.Serializable; 6 | 7 | public class Account implements Serializable { 8 | private Integer id; 9 | private String name; 10 | private Float money; 11 | 12 | public Integer getId() { 13 | return id; 14 | } 15 | 16 | public void setId(Integer id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public Float getMoney() { 29 | return money; 30 | } 31 | 32 | public void setMoney(Float money) { 33 | this.money = money; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "Account{" + 39 | "id=" + id + 40 | ", name='" + name + '\'' + 41 | ", money=" + money + 42 | '}'; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/src/main/java/com/mine/jdbctemplate/JdbcTemplateDemo1.java: -------------------------------------------------------------------------------- 1 | package com.mine.jdbctemplate; 2 | 3 | import com.mine.dao.IAccountDao; 4 | import com.mine.domain.Account; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.context.support.ClassPathXmlApplicationContext; 7 | import org.springframework.jdbc.core.BeanPropertyRowMapper; 8 | import org.springframework.jdbc.core.JdbcTemplate; 9 | import org.springframework.jdbc.core.RowMapper; 10 | import org.springframework.jdbc.datasource.DriverManagerDataSource; 11 | 12 | import java.sql.ResultSet; 13 | import java.sql.SQLException; 14 | import java.util.List; 15 | 16 | public class JdbcTemplateDemo1 { 17 | public static void main(String[] args) { 18 | ClassPathXmlApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 19 | IAccountDao accountDao = cls.getBean("accountDao", IAccountDao.class); 20 | Account account = accountDao.getAccountById(8); 21 | // Account account = accountDao.getAccountByName("阿珂"); 22 | // account.setMoney(777f); 23 | // accountDao.updateAccount(account); 24 | System.out.println(account); 25 | 26 | 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /demo/spring中jdbcTemplate的使用/src/main/resources/bean.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | aop01 8 | aop01 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 17 | 6 18 | 6 19 | 20 | 21 | 22 | 23 | jar 24 | 25 | 26 | 27 | org.springframework 28 | spring-context 29 | 5.0.2.RELEASE 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 1.8.7 35 | 36 | 37 | org.springframework 38 | spring-test 39 | 5.0.2.RELEASE 40 | 41 | 42 | commons-dbutils 43 | commons-dbutils 44 | 1.4 45 | 46 | 47 | 48 | mysql 49 | mysql-connector-java 50 | 5.1.6 51 | 52 | 53 | 54 | c3p0 55 | c3p0 56 | 0.9.1.2 57 | 58 | 59 | 60 | junit 61 | junit 62 | 4.12 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/main/java/com/mine/dao/IAccountDao.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | public interface IAccountDao { 4 | } 5 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/main/java/com/mine/dao/impl/AccountDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao.impl; 2 | 3 | import com.mine.dao.IAccountDao; 4 | 5 | public class AccountDaoImpl implements IAccountDao { 6 | } 7 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/main/java/com/mine/domain/Account.java: -------------------------------------------------------------------------------- 1 | package com.mine.domain; 2 | 3 | import java.io.PrintStream; 4 | import java.io.Serializable; 5 | 6 | public class Account implements Serializable { 7 | private Integer id; 8 | private String name; 9 | private Float money; 10 | 11 | public Integer getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Integer id) { 16 | this.id = id; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public Float getMoney() { 28 | return money; 29 | } 30 | 31 | public void setMoney(Float money) { 32 | this.money = money; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "Account{" + 38 | "id=" + id + 39 | ", name='" + name + '\'' + 40 | ", money=" + money + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/main/java/com/mine/service/IAccountService.java: -------------------------------------------------------------------------------- 1 | package com.mine.service; 2 | 3 | public interface IAccountService { 4 | 5 | void saveAccount(); 6 | 7 | void updateAccount(int i); 8 | 9 | int deleteAccount(int i); 10 | } 11 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/main/java/com/mine/service/impl/AccountServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.service.impl; 2 | 3 | import com.mine.service.IAccountService; 4 | 5 | public class AccountServiceImpl implements IAccountService { 6 | @Override 7 | public void saveAccount() { 8 | System.out.println("执行了保存"); 9 | } 10 | 11 | @Override 12 | public void updateAccount(int i) { 13 | System.out.println("执行了更新" + i); 14 | 15 | } 16 | 17 | @Override 18 | public int deleteAccount(int i) { 19 | System.out.println("执行了删除" + i); 20 | return 0; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/main/java/com/mine/utils/Logger.java: -------------------------------------------------------------------------------- 1 | package com.mine.utils; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | 5 | /** 6 | * 用于记录日志的工具类 7 | */ 8 | public class Logger { 9 | 10 | /** 11 | * 前置通知 12 | */ 13 | public void beforePrintLog() { 14 | System.out.println("前置通知logger中的printLog方法开始执行了..."); 15 | } 16 | 17 | /** 18 | * 后置通知 19 | */ 20 | public void afterReturningPrintLog() { 21 | System.out.println("后置通知logger中的printLog方法开始执行了..."); 22 | } 23 | 24 | /** 25 | * 异常通知 26 | */ 27 | public void afterThrowingPrintLog() { 28 | System.out.println("异常通知logger中的printLog方法开始执行了..."); 29 | } 30 | 31 | /** 32 | * 最终通知 33 | */ 34 | public void afterPrintLog() { 35 | System.out.println("最终通知logger中的printLog方法开始执行了..."); 36 | } 37 | 38 | public Object aroundPringLog(ProceedingJoinPoint pjp) { 39 | Object resValue = null; 40 | try { 41 | Object[] args = pjp.getArgs(); // 获取方法执行所需要的参数 42 | System.out.println("aroundPringLog中的logger方法开始记录日志了...前置"); 43 | resValue = pjp.proceed(args); // 明确调用业务层的切入点方法 ....切入点方法 44 | System.out.println("aroundPringLog中的logger方法开始记录日志了...后置"); 45 | return resValue; 46 | } catch (Throwable throwable) { 47 | System.out.println("aroundPringLog中的logger方法开始记录日志了...异常"); 48 | throw new RuntimeException(throwable); 49 | } finally { 50 | System.out.println("aroundPringLog中的logger方法开始记录日志了...最终"); 51 | 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/main/resources/bean.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /demo/spring基于xml的Aop配置/src/test/java/com/mine/test/AopTest.java: -------------------------------------------------------------------------------- 1 | package com.mine.test; 2 | 3 | import com.mine.service.IAccountService; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class AopTest { 7 | public static void main(String[] args) { 8 | ClassPathXmlApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 9 | IAccountService accountService = cls.getBean("accountService", IAccountService.class); 10 | accountService.saveAccount(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/demo01.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | demo01 8 | demo01 9 | 1.0-SNAPSHOT 10 | jar 11 | 12 | 13 | 14 | org.springframework 15 | spring-context 16 | 5.0.8.RELEASE 17 | 18 | 19 | commons-dbutils 20 | commons-dbutils 21 | 1.6 22 | 23 | 24 | mysql 25 | mysql-connector-java 26 | 8.0.15 27 | 28 | 29 | com.mchange 30 | c3p0 31 | 0.9.5.2 32 | 33 | 34 | junit 35 | junit 36 | 4.11 37 | test 38 | 39 | 40 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/src/main/java/com/mine/dao/IAccountDao.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.domain.Account; 4 | 5 | import java.util.List; 6 | 7 | public interface IAccountDao { 8 | /** 9 | * 查询所有数据 10 | * @return 11 | */ 12 | List findAllAccount(); 13 | 14 | /** 15 | * 根据id查询对应账户 16 | * @param accountId 17 | * @return 18 | */ 19 | Account findAccountById(Integer accountId); 20 | 21 | /** 22 | * 保存账户 23 | * @param account 24 | */ 25 | void saveAccount(Account account); 26 | 27 | /** 28 | * 更新 29 | * @param account 30 | */ 31 | void updateAccount (Account account); 32 | 33 | /** 34 | * 根据id删除账户 35 | * @param accountId 36 | */ 37 | void deleteAccountById(Integer accountId); 38 | } 39 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/src/main/java/com/mine/dao/impl/AccountDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao.impl; 2 | 3 | import com.mine.dao.IAccountDao; 4 | import com.mine.domain.Account; 5 | import org.apache.commons.dbutils.QueryRunner; 6 | import org.apache.commons.dbutils.handlers.BeanHandler; 7 | import org.apache.commons.dbutils.handlers.BeanListHandler; 8 | import sun.plugin2.main.server.ResultHandler; 9 | 10 | import java.util.List; 11 | 12 | public class AccountDaoImpl implements IAccountDao { 13 | private QueryRunner runner; 14 | 15 | public AccountDaoImpl(QueryRunner runner) { 16 | this.runner = runner; 17 | } 18 | 19 | public List findAllAccount() { 20 | try { 21 | return runner.query("select * from count ", new BeanListHandler(Account.class)); 22 | } catch (Exception e) { 23 | throw new RuntimeException(); 24 | } 25 | } 26 | 27 | public Account findAccountById(Integer accountId) { 28 | try { 29 | return runner.query("select *from count where id = ?", new BeanHandler(Account.class), accountId); 30 | } catch (Exception e) { 31 | throw new RuntimeException(); 32 | } 33 | } 34 | public void saveAccount(Account account) { 35 | try { 36 | runner.update("insert into count (name, money) values (?, ?)",account.getName(),account.getMoney() ); 37 | } catch (Exception e) { 38 | throw new RuntimeException(); 39 | } 40 | } 41 | 42 | public void updateAccount(Account account) { 43 | try { 44 | runner.update("update count set name = ?, money = ? where id = ?",account.getName(), account.getMoney(), account.getId()); 45 | } catch (Exception e) { 46 | throw new RuntimeException(); 47 | } 48 | } 49 | 50 | public void deleteAccountById(Integer accountId) { 51 | try { 52 | runner.update("delete from count where id = ?", accountId); 53 | } catch (Exception e) { 54 | throw new RuntimeException(); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/src/main/java/com/mine/domain/Account.java: -------------------------------------------------------------------------------- 1 | package com.mine.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Account implements Serializable { 6 | private Integer id; 7 | private String name; 8 | private Float money; 9 | 10 | public Integer getId() { 11 | return id; 12 | } 13 | 14 | public void setId(Integer id) { 15 | this.id = id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public Float getMoney() { 27 | return money; 28 | } 29 | 30 | public void setMoney(Float money) { 31 | this.money = money; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "account{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | ", money=" + money + 40 | '}'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/src/main/java/com/mine/service/IAccountService.java: -------------------------------------------------------------------------------- 1 | package com.mine.service; 2 | 3 | import com.mine.domain.Account; 4 | 5 | import java.util.List; 6 | 7 | public interface IAccountService { 8 | /** 9 | * 查询所有数据 10 | * @return 11 | */ 12 | List findAllAccount(); 13 | 14 | /** 15 | * 根据id查询对应账户 16 | * @param accountId 17 | * @return 18 | */ 19 | Account findAccountById(Integer accountId); 20 | 21 | /** 22 | * 保存账户 23 | * @param account 24 | */ 25 | void saveAccount(Account account); 26 | 27 | /** 28 | * 更新 29 | * @param account 30 | */ 31 | void updateAccount (Account account); 32 | 33 | /** 34 | * 根据id删除账户 35 | * @param accountId 36 | */ 37 | void deleteAccountById(Integer accountId); 38 | } 39 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/src/main/java/com/mine/service/impl/AccountServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.service.impl; 2 | 3 | import com.mine.dao.IAccountDao; 4 | import com.mine.domain.Account; 5 | import com.mine.service.IAccountService; 6 | 7 | import java.util.List; 8 | 9 | public class AccountServiceImpl implements IAccountService { 10 | private IAccountDao accountDao; 11 | 12 | 13 | public void setAccountDao(IAccountDao accountDao) { 14 | this.accountDao = accountDao; 15 | } 16 | 17 | public List findAllAccount() { 18 | return accountDao.findAllAccount(); 19 | } 20 | 21 | public Account findAccountById(Integer accountId) { 22 | return accountDao.findAccountById(accountId); 23 | } 24 | 25 | public void saveAccount(Account account) { 26 | accountDao.saveAccount(account); 27 | } 28 | 29 | public void updateAccount(Account account) { 30 | accountDao.updateAccount(account); 31 | 32 | } 33 | 34 | public void deleteAccountById(Integer accountId) { 35 | accountDao.deleteAccountById(accountId); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/src/main/resources/bean.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /demo/spring基于xml的Ioc配置/src/test/java/com/mine/test.java: -------------------------------------------------------------------------------- 1 | package com.mine; 2 | 3 | import com.mine.domain.Account; 4 | import com.mine.service.IAccountService; 5 | import org.junit.Test; 6 | import org.omg.CORBA.PUBLIC_MEMBER; 7 | import org.springframework.context.ApplicationContext; 8 | import org.springframework.context.support.ClassPathXmlApplicationContext; 9 | 10 | import java.util.List; 11 | 12 | public class test { 13 | @Test 14 | public void findAllAccount() { 15 | ApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 16 | IAccountService accountService = cls.getBean("accountService", IAccountService.class); 17 | List allAccount = accountService.findAllAccount(); 18 | for (Account account : allAccount) { 19 | System.out.println(account ); 20 | } 21 | } 22 | 23 | @Test 24 | public void findOne() { 25 | ApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 26 | IAccountService accountService = cls.getBean("accountService", IAccountService.class); 27 | Account account = accountService.findAccountById(3); 28 | System.out.println(account); 29 | } 30 | @Test 31 | public void saveTest() { 32 | Account account = new Account(); 33 | account.setMoney(9988f); 34 | account.setName("孙尚香"); 35 | ApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 36 | IAccountService accountService = cls.getBean("accountService", IAccountService.class); 37 | accountService.saveAccount(account); 38 | } 39 | 40 | @Test 41 | public void updateTest() { 42 | ApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 43 | IAccountService accountService = cls.getBean("accountService", IAccountService.class); 44 | Account account = accountService.findAccountById(4); 45 | account.setMoney(1990f); 46 | accountService.updateAccount(account); 47 | } 48 | @Test 49 | public void deleteTest() { 50 | ApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 51 | IAccountService accountService = cls.getBean("accountService", IAccountService.class); 52 | accountService.deleteAccountById(1); 53 | } 54 | } 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | springTest 8 | springTest 9 | 1.0-SNAPSHOT 10 | jar 11 | 12 | 13 | 14 | org.springframework 15 | spring-context 16 | 5.1.5.RELEASE 17 | 18 | 19 | org.springframework 20 | spring-jdbc 21 | 5.1.5.RELEASE 22 | 23 | 24 | org.springframework 25 | spring-tx 26 | 5.0.8.RELEASE 27 | 28 | 29 | mysql 30 | mysql-connector-java 31 | 8.0.11 32 | 33 | 34 | org.aspectj 35 | aspectjweaver 36 | 1.9.2 37 | 38 | 39 | junit 40 | junit 41 | 4.12 42 | test 43 | 44 | 45 | org.springframework 46 | spring-test 47 | 5.1.8.RELEASE 48 | test 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/springTest.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/src/main/java/com/mine/dao/IAccountDao.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.domain.Account; 4 | 5 | public interface IAccountDao { 6 | 7 | Account getAccountById(Integer accountId); 8 | 9 | Account getAccountByName(String accountName); 10 | 11 | void updateAccount(Account account); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/src/main/java/com/mine/dao/impl/AccountImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao.impl; 2 | 3 | import com.mine.dao.IAccountDao; 4 | import com.mine.domain.Account; 5 | import org.springframework.jdbc.core.BeanPropertyRowMapper; 6 | import org.springframework.jdbc.core.support.JdbcDaoSupport; 7 | 8 | import java.util.List; 9 | 10 | public class AccountImpl extends JdbcDaoSupport implements IAccountDao { 11 | public Account getAccountById(Integer accountId) { 12 | List accounts = getJdbcTemplate().query("select * from count where id = ?", new BeanPropertyRowMapper(Account.class),accountId); 13 | return accounts.isEmpty() ? null : accounts.get(0); 14 | } 15 | 16 | public Account getAccountByName(String accountName) { 17 | List accounts = getJdbcTemplate().query("select * from count where name = ?", new BeanPropertyRowMapper(Account.class),accountName); 18 | if (accounts.isEmpty()) { 19 | return null; 20 | } 21 | if (accounts.size() > 1) { 22 | throw new RuntimeException("查询结果不唯一"); 23 | } 24 | return accounts.get(0); 25 | } 26 | 27 | public void updateAccount(Account account) { 28 | getJdbcTemplate().update("update count set name = ?, money = ? where id = ?",account.getName(),account.getMoney(),account.getId()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/src/main/java/com/mine/domain/Account.java: -------------------------------------------------------------------------------- 1 | package com.mine.domain; 2 | 3 | import org.springframework.jdbc.core.ResultSetExtractor; 4 | 5 | import java.io.Serializable; 6 | 7 | public class Account implements Serializable { 8 | private Integer id; 9 | private String name; 10 | private Float money; 11 | 12 | public Integer getId() { 13 | return id; 14 | } 15 | 16 | public void setId(Integer id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public Float getMoney() { 29 | return money; 30 | } 31 | 32 | public void setMoney(Float money) { 33 | this.money = money; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "Account{" + 39 | "id=" + id + 40 | ", name='" + name + '\'' + 41 | ", money=" + money + 42 | '}'; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/src/main/java/com/mine/service/IAccountService.java: -------------------------------------------------------------------------------- 1 | package com.mine.service; 2 | 3 | import com.mine.domain.Account; 4 | 5 | public interface IAccountService { 6 | /** 7 | * 查询 8 | * @param accountId 9 | * @return 10 | */ 11 | Account getAccountById(Integer accountId); 12 | 13 | /** 14 | * 转账 15 | * @param sourceName 转出 16 | * @param targetName 目标账户 17 | * @param money 转账金额 18 | */ 19 | void transfer(String sourceName, String targetName, Float money); 20 | } 21 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/src/main/java/com/mine/service/impl/AccountServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.service.impl; 2 | 3 | import com.mine.dao.IAccountDao; 4 | import com.mine.dao.impl.AccountImpl; 5 | import com.mine.domain.Account; 6 | import com.mine.service.IAccountService; 7 | 8 | public class AccountServiceImpl implements IAccountService { 9 | 10 | private IAccountDao accountDao; 11 | 12 | public void setAccountDao(IAccountDao accountDao) { 13 | this.accountDao = accountDao; 14 | } 15 | 16 | public Account getAccountById(Integer accountId) { 17 | return accountDao.getAccountById(accountId); 18 | } 19 | 20 | public void transfer(String sourceName, String targetName, Float money) { 21 | Account source = accountDao.getAccountByName(sourceName); 22 | Account target = accountDao.getAccountByName(targetName); 23 | source.setMoney(source.getMoney() - money); 24 | target.setMoney(target.getMoney() + money); 25 | accountDao.updateAccount(source); 26 | accountDao.updateAccount(target); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/src/main/resources/bean.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 33 | 34 | 35 | 36 | 37 | 39 | 40 | 41 | 42 | 43 | 44 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /demo/spring基于xml的声明式事务控制/src/test/java/com/mine/test/SpringTest.java: -------------------------------------------------------------------------------- 1 | package com.mine.test; 2 | 3 | import com.mine.service.IAccountService; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | @RunWith(SpringJUnit4ClassRunner.class) 11 | @ContextConfiguration(locations = {"classpath:bean.xml"}) 12 | public class SpringTest { 13 | @Autowired 14 | private IAccountService accountService; 15 | 16 | @Test 17 | public void transfer() { 18 | accountService.transfer("ddd","孙尚香",10f); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /demo/ssm整合/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | com.mine 8 | ssm 9 | 1.0-SNAPSHOT 10 | war 11 | 12 | ssm Maven Webapp 13 | 14 | http://www.example.com 15 | 16 | 17 | UTF-8 18 | 1.8 19 | 1.8 20 | 5.0.2.RELEASE 21 | 1.6.6 22 | 1.2.12 23 | 5.1.6 24 | 3.4.5 25 | 26 | 27 | 28 | 29 | org.aspectj 30 | aspectjweaver 31 | 1.6.8 32 | 33 | 34 | org.springframework 35 | spring-aop 36 | ${spring.version} 37 | 38 | 39 | org.springframework 40 | spring-context 41 | ${spring.version} 42 | 43 | 44 | org.springframework 45 | spring-web 46 | ${spring.version} 47 | 48 | 49 | org.springframework 50 | spring-webmvc 51 | ${spring.version} 52 | 53 | 54 | org.springframework 55 | spring-test 56 | ${spring.version} 57 | 58 | 59 | org.springframework 60 | spring-tx 61 | ${spring.version} 62 | 63 | 64 | org.springframework 65 | spring-jdbc 66 | ${spring.version} 67 | 68 | 69 | junit 70 | junit 71 | 4.12 72 | compile 73 | 74 | 75 | mysql 76 | mysql-connector-java 77 | 5.1.46 78 | 79 | 80 | javax.servlet 81 | servlet-api 82 | 2.5 83 | provided 84 | 85 | 86 | javax.servlet.jsp 87 | jsp-api 88 | 2.0 89 | provided 90 | 91 | 92 | jstl 93 | jstl 94 | 1.2 95 | 96 | 97 | 98 | log4j 99 | log4j 100 | ${log4j.version} 101 | 102 | 103 | org.slf4j 104 | slf4j-api 105 | ${slf4j.version} 106 | 107 | 108 | org.slf4j 109 | slf4j-log4j12 110 | ${slf4j.version} 111 | 112 | 113 | 114 | org.mybatis 115 | mybatis 116 | ${mybatis.version} 117 | 118 | 119 | org.mybatis 120 | mybatis-spring 121 | 1.3.0 122 | 123 | 124 | c3p0 125 | c3p0 126 | 0.9.1.2 127 | jar 128 | compile 129 | 130 | 131 | 132 | 133 | ssm 134 | 135 | 136 | 137 | maven-clean-plugin 138 | 3.1.0 139 | 140 | 141 | 142 | maven-resources-plugin 143 | 3.0.2 144 | 145 | 146 | maven-compiler-plugin 147 | 3.8.0 148 | 149 | 150 | maven-surefire-plugin 151 | 2.22.1 152 | 153 | 154 | maven-war-plugin 155 | 3.2.2 156 | 157 | 158 | maven-install-plugin 159 | 2.5.2 160 | 161 | 162 | maven-deploy-plugin 163 | 2.8.2 164 | 165 | 166 | 167 | 168 | 169 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/java/com/mine/controller/AccountController.java: -------------------------------------------------------------------------------- 1 | package com.mine.controller; 2 | 3 | import com.mine.domain.Account; 4 | import com.mine.service.AccountService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.ui.Model; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | import java.util.List; 13 | 14 | // web 15 | @Controller 16 | @RequestMapping("/account") 17 | public class AccountController { 18 | @Autowired 19 | private AccountService accountService; 20 | @RequestMapping("/findAll") 21 | public String findAll(Model model) { 22 | System.out.println("表现层方法调用"); 23 | List list = accountService.findAll(); 24 | // accountService.savaAccount(new Account()); 25 | model.addAttribute("list",list); 26 | return "success"; 27 | } 28 | 29 | @RequestMapping("/saveAccount") 30 | public void saveAccount(HttpServletRequest request, HttpServletResponse response, Account account) throws Exception{ 31 | accountService.savaAccount(account); 32 | response.sendRedirect(request.getContextPath() + "/account/findAll"); 33 | return ; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/java/com/mine/dao/AccountDao.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.domain.Account; 4 | import org.apache.ibatis.annotations.Insert; 5 | import org.apache.ibatis.annotations.Select; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | @Repository 10 | public interface AccountDao { 11 | // 查询 12 | @Select("select *from account") 13 | public List findAll(); 14 | // 保存 15 | @Insert("insert into account (name, money) values (#{name}, #{money})") 16 | public void savaAccount(Account account); 17 | } 18 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/java/com/mine/domain/Account.java: -------------------------------------------------------------------------------- 1 | package com.mine.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Account implements Serializable { 6 | private Integer id; 7 | private String name; 8 | private Double money; 9 | 10 | public Integer getId() { 11 | return id; 12 | } 13 | 14 | public void setId(Integer id) { 15 | this.id = id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public Double getMoney() { 27 | return money; 28 | } 29 | 30 | public void setMoney(Double money) { 31 | this.money = money; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "Account{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | ", money=" + money + 40 | '}'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/java/com/mine/service/AccountService.java: -------------------------------------------------------------------------------- 1 | package com.mine.service; 2 | 3 | import com.mine.domain.Account; 4 | 5 | import java.util.List; 6 | 7 | public interface AccountService { 8 | // 查询 9 | public List findAll(); 10 | // 保存 11 | public void savaAccount(Account account); 12 | } 13 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/java/com/mine/service/impl/AccountServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.mine.service.impl; 2 | 3 | import com.mine.dao.AccountDao; 4 | import com.mine.domain.Account; 5 | import com.mine.service.AccountService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service("accountService") 12 | public class AccountServiceImpl implements AccountService { 13 | @Autowired 14 | private AccountDao accountDao; 15 | public List findAll() { 16 | System.out.println("业务层 : 查询所有账户信息"); 17 | return accountDao.findAll(); 18 | } 19 | 20 | public void savaAccount(Account account) { 21 | System.out.println("业务成: 保存账户"); 22 | accountDao.savaAccount(account); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/java/com/mine/test/MybatisTest.java: -------------------------------------------------------------------------------- 1 | package com.mine.test; 2 | 3 | import com.mine.dao.AccountDao; 4 | import com.mine.domain.Account; 5 | import org.apache.ibatis.io.Resources; 6 | import org.apache.ibatis.session.SqlSession; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 | import org.junit.Test; 10 | 11 | import java.io.IOException; 12 | import java.io.InputStream; 13 | import java.util.List; 14 | 15 | public class MybatisTest { 16 | /* 17 | @Test 18 | public void testFindAll() throws Exception { 19 | InputStream resource = Resources.getResourceAsStream("MapperConfig.xml"); 20 | SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource); 21 | SqlSession sqlSession = sessionFactory.openSession(); 22 | AccountDao accountDao = sqlSession.getMapper(AccountDao.class); 23 | List list = accountDao.findAll(); 24 | System.out.println(list); 25 | sqlSession.commit(); 26 | sqlSession.close(); 27 | resource.close(); 28 | } 29 | 30 | @Test 31 | public void saveAccount() throws Exception { 32 | Account account = new Account(); 33 | account.setName("李白"); 34 | account.setMoney(700d); 35 | InputStream resource = Resources.getResourceAsStream("MapperConfig.xml"); 36 | SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource); 37 | SqlSession sqlSession = sessionFactory.openSession(); 38 | AccountDao accountDao = sqlSession.getMapper(AccountDao.class); 39 | accountDao.savaAccount(account); 40 | sqlSession.commit(); 41 | sqlSession.close(); 42 | resource.close(); 43 | } 44 | */ 45 | } 46 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/java/com/mine/test/SpringTest.java: -------------------------------------------------------------------------------- 1 | package com.mine.test; 2 | 3 | import com.mine.domain.Account; 4 | import com.mine.service.AccountService; 5 | import org.junit.Test; 6 | import org.springframework.context.support.ClassPathXmlApplicationContext; 7 | 8 | public class SpringTest { 9 | @Test 10 | public void testSpring() { 11 | ClassPathXmlApplicationContext cls = new ClassPathXmlApplicationContext("applicationContext.xml"); 12 | AccountService accountService = cls.getBean("accountService", AccountService.class); 13 | accountService.findAll(); 14 | accountService.savaAccount(new Account()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | ### 配置根 ### 2 | log4j.rootLogger = DEBUG,console ,file 3 | 4 | ### 配置输出到控制台 ### 5 | log4j.appender.console = org.apache.log4j.ConsoleAppender 6 | log4j.appender.console.Target = System.out 7 | log4j.appender.console.Threshold = DEBUG 8 | log4j.appender.console.layout = org.apache.log4j.PatternLayout 9 | log4j.appender.console.layout.ConversionPattern = [%c] - %m%n 10 | 11 | ### 配置输出到文件 ### 12 | log4j.appender.file = org.apache.log4j.RollingFileAppender 13 | log4j.appender.file.File = ./logs/log4jTest.log 14 | log4j.appender.file.maxFileSize = 10mb 15 | #log4j.appender.fileAppender.Append = true 16 | log4j.appender.file.Threshold = DEBUG 17 | log4j.appender.file.layout = org.apache.log4j.PatternLayout 18 | log4j.appender.file.layout.ConversionPattern = [%p][%d{yy-MM-dd}][%c]%m%n 19 | 20 | ### 设置输出sql的级别,其中logger后面的内容全部为jar包中所包含的包名 ### 21 | log4j.logger.org.apache = DEBUG 22 | log4j.logger.org.mybatis = DEBUG 23 | log4j.logger.java.sql.Connection = DEBUG 24 | log4j.logger.java.sql.Statement = DEBUG 25 | log4j.logger.java.sql.PreparedStatement = DEBUG 26 | log4j.logger.java.sql.ResultSet = DEBUG -------------------------------------------------------------------------------- /demo/ssm整合/src/main/resources/springmvc.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/webapp/WEB-INF/pages/success.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: chentao 4 | Date: 2019/11/5 5 | Time: 6:23 下午 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> 9 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 10 | 11 | 12 | Title 13 | 14 | 15 |

查询所有用户信息

16 |
17 | 18 | ${account.name} 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | org.springframework.web.context.ContextLoaderListener 8 | 9 | 10 | contextConfigLocation 11 | classpath:applicationContext.xml 12 | 13 | Archetype Created Web Application 14 | 15 | dispatcherServlet 16 | org.springframework.web.servlet.DispatcherServlet 17 | 18 | contextConfigLocation 19 | classpath:springmvc.xml 20 | 21 | 1 22 | 23 | 24 | dispatcherServlet 25 | / 26 | 27 | 28 | characterEncodingFilter 29 | org.springframework.web.filter.CharacterEncodingFilter 30 | 31 | encoding 32 | UTF-8 33 | 34 | 35 | 36 | characterEncodingFilter 37 | /* 38 | 39 | 40 | -------------------------------------------------------------------------------- /demo/ssm整合/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: chentao 4 | Date: 2019/11/5 5 | Time: 6:23 下午 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | Title 12 | 13 | 14 | 15 | 测试ssm的获取 16 | 17 |
18 |

测试保存

19 |
20 | 姓名:
21 | 金额:
22 |
23 |
24 | 25 | 26 | -------------------------------------------------------------------------------- /demo/ssm整合/ssm.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /demo/staff/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.10.RELEASE 9 | 10 | 11 | com.mine 12 | staff 13 | 0.0.1-SNAPSHOT 14 | staff 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-thymeleaf 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | 38 | org.thymeleaf 39 | thymeleaf-spring5 40 | 41 | 42 | 43 | org.thymeleaf.extras 44 | thymeleaf-extras-java8time 45 | 46 | 47 | org.mybatis.spring.boot 48 | mybatis-spring-boot-starter 49 | 2.1.0 50 | 51 | 52 | org.projectlombok 53 | lombok 54 | provided 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-maven-plugin 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/StaffApplication.java: -------------------------------------------------------------------------------- 1 | package com.mine; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class StaffApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(StaffApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/config/LoginHandlerInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.mine.config; 2 | 3 | import org.springframework.util.StringUtils; 4 | import org.springframework.web.servlet.HandlerInterceptor; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | public class LoginHandlerInterceptor implements HandlerInterceptor { 11 | @Override 12 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 13 | Object loginUser = request.getSession().getAttribute("loginUser"); 14 | if (loginUser == null) { 15 | request.setAttribute("msg","没有权限, 请先登录!"); 16 | request.getRequestDispatcher("/index.html").forward(request, response); 17 | return false; 18 | } else { 19 | return true; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/config/MyLocalResolver.java: -------------------------------------------------------------------------------- 1 | package com.mine.config; 2 | 3 | import org.springframework.util.StringUtils; 4 | import org.springframework.web.servlet.LocaleResolver; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.util.Locale; 9 | 10 | public class MyLocalResolver implements LocaleResolver { 11 | @Override 12 | public Locale resolveLocale(HttpServletRequest request) { 13 | String language = request.getParameter("l"); 14 | Locale locale = Locale.getDefault(); 15 | if (!StringUtils.isEmpty(language)) { 16 | String[] split = language.split("_"); 17 | locale = new Locale(split[0],split[1]); 18 | } 19 | return locale; 20 | } 21 | 22 | @Override 23 | public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/config/MyMvcConfig.java: -------------------------------------------------------------------------------- 1 | package com.mine.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.LocaleResolver; 6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 7 | import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 9 | 10 | @Configuration 11 | public class MyMvcConfig implements WebMvcConfigurer { 12 | @Override 13 | public void addViewControllers(ViewControllerRegistry registry) { 14 | registry.addViewController("/").setViewName("index"); 15 | registry.addViewController("/index.html").setViewName("index"); 16 | registry.addViewController("/main.html").setViewName("dashboard"); 17 | } 18 | 19 | @Bean 20 | public LocaleResolver localeResolver() { 21 | return new MyLocalResolver(); 22 | } 23 | 24 | @Override 25 | public void addInterceptors(InterceptorRegistry registry) { 26 | registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**") 27 | .excludePathPatterns("/index.html","/","/user/login","/css/**","/js/**","/img/**"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/controller/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package com.mine.controller; 2 | 3 | import com.mine.dao.DepartmentDao; 4 | import com.mine.dao.EmployeeDao; 5 | import com.mine.pojo.Department; 6 | import com.mine.pojo.Employee; 7 | import com.sun.org.apache.xpath.internal.operations.Mod; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.ui.Model; 11 | import org.springframework.web.bind.annotation.GetMapping; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.PostMapping; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | 16 | import java.util.Collection; 17 | 18 | @Controller 19 | public class EmployeeController { 20 | 21 | @Autowired 22 | private EmployeeDao employeeDao; 23 | @Autowired 24 | private DepartmentDao departmentDao; 25 | @RequestMapping("/emp") 26 | public String List(Model model) { 27 | Collection employees = employeeDao.getAll(); 28 | model.addAttribute("emps",employees); 29 | return "emp/list"; 30 | } 31 | 32 | @GetMapping("/add") 33 | public String toAddpage(Model model) { 34 | Collection departments = departmentDao.getDepartments(); 35 | model.addAttribute("departments",departments); 36 | return "emp/add"; 37 | } 38 | 39 | @PostMapping("/add") 40 | public String addEmp(Employee employee) { 41 | employeeDao.save(employee); 42 | return "redirect:/emp"; 43 | } 44 | // 编辑 45 | @GetMapping("/emp/{id}") 46 | public String updateEmp(@PathVariable("id")Integer id, Model model) { 47 | Employee employee = employeeDao.getEmployeeById(id); 48 | model.addAttribute("emp", employee); 49 | Collection departments = departmentDao.getDepartments(); 50 | model.addAttribute("departments",departments); 51 | return "emp/update"; 52 | } 53 | 54 | @PostMapping("/updateEmp") 55 | public String updateChanged(Employee employee) { 56 | employeeDao.save(employee); 57 | return "redirect:/emp"; 58 | } 59 | 60 | @GetMapping("/delete/{id}") 61 | public String deleteEmp(@PathVariable("id") Integer id) { 62 | employeeDao.deleteById(id); 63 | return "redirect:/emp"; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/controller/LoginController.java: -------------------------------------------------------------------------------- 1 | package com.mine.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.Model; 5 | import org.springframework.util.StringUtils; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestParam; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | 10 | import javax.servlet.http.HttpSession; 11 | 12 | @Controller 13 | public class LoginController { 14 | 15 | @RequestMapping("/user/login") 16 | // @ResponseBody 17 | public String login(@RequestParam("username") String username, 18 | @RequestParam("password") String password, 19 | HttpSession session, 20 | Model model) { 21 | if (!StringUtils.isEmpty(username) && "123".equals(password)) { 22 | session.setAttribute("loginUser", username); 23 | return "redirect:/main.html"; 24 | } else { 25 | model.addAttribute("msg","用户名或密码错误"); 26 | return "index"; 27 | } 28 | } 29 | 30 | // 注销 31 | @RequestMapping("/user/loginOut") 32 | public String loginOut(HttpSession session) { 33 | session.invalidate(); 34 | return "redirect:/index.html"; 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/dao/DepartmentDao.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.pojo.Department; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import java.util.Collection; 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | @Repository 11 | public class DepartmentDao { 12 | private static Map departments = null; 13 | static { 14 | departments = new HashMap(); 15 | departments.put(101, new Department(101,"政教处")); 16 | departments.put(102, new Department(102,"市场部")); 17 | departments.put(103, new Department(103, "后勤部")); 18 | departments.put(104, new Department(104, "教务处")); 19 | departments.put(105, new Department(105, "行政部")); 20 | departments.put(106, new Department(106, "财务部")); 21 | } 22 | 23 | public Collection getDepartments() { 24 | return departments.values(); 25 | } 26 | 27 | public Department getDepartmentById(Integer id) { 28 | return departments.get(id); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/dao/EmployeeDao.java: -------------------------------------------------------------------------------- 1 | package com.mine.dao; 2 | 3 | import com.mine.pojo.Department; 4 | import com.mine.pojo.Employee; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Repository; 9 | 10 | import java.util.Collection; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | @Repository 15 | public class EmployeeDao { 16 | 17 | @Autowired 18 | private DepartmentDao departmentDao; 19 | private static Map employees = null; 20 | static { 21 | employees = new HashMap(); 22 | employees.put(1001,new Employee(1001,"张三","938723ds@163.com",1,new Department(101,"政教处"))); 23 | employees.put(1002,new Employee(1002,"阿模块 ","4dfdg@163.com",0,new Department(102,"市场部"))); 24 | employees.put(1003,new Employee(1003,"水电费","234vfdfg@163.com",1,new Department(103,"后勤处"))); 25 | employees.put(1004,new Employee(1004,"偶回家","134dffg@163.com",0,new Department(104,"教务处"))); 26 | employees.put(1005,new Employee(1005,"二次","0092f@163.com",1,new Department(105,"行政部"))); 27 | } 28 | 29 | private static Integer initId =1006; 30 | public void save(Employee employee) { 31 | if (employee.getId() == null) { 32 | employee.setId(initId++); 33 | } 34 | employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId())); 35 | employees.put(employee.getId(),employee); 36 | } 37 | 38 | public Collection getAll() { 39 | return employees.values(); 40 | } 41 | 42 | 43 | public Employee getEmployeeById(Integer id) { 44 | return employees.get(id); 45 | } 46 | 47 | public void deleteById(Integer id) { 48 | employees.remove(id); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/pojo/Department.java: -------------------------------------------------------------------------------- 1 | package com.mine.pojo; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @NoArgsConstructor 9 | @AllArgsConstructor 10 | public class Department { 11 | private Integer id; 12 | private String departmentName; 13 | } 14 | -------------------------------------------------------------------------------- /demo/staff/src/main/java/com/mine/pojo/Employee.java: -------------------------------------------------------------------------------- 1 | package com.mine.pojo; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Date; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | public class Employee { 12 | private Integer id; 13 | private String lastName; 14 | private String email; 15 | private Integer gender; 16 | private Department department; 17 | private Date birth; 18 | 19 | public Employee(Integer id, String lastName, String email, Integer gender, Department department) { 20 | this.id = id; 21 | this.lastName = lastName; 22 | this.email = email; 23 | this.gender = gender; 24 | this.department = department; 25 | this.birth = new Date(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.thymeleaf.cache=false 2 | spring.messages.basename=i18n.login 3 | spring.mvc.date-format=yyyy-MM-dd 4 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/i18n/login.properties: -------------------------------------------------------------------------------- 1 | login.btn=登录 2 | login.password=密码 3 | login.remember=记住我 4 | login.tip=请登录 5 | login.username=用户名 -------------------------------------------------------------------------------- /demo/staff/src/main/resources/i18n/login_en_US.properties: -------------------------------------------------------------------------------- 1 | login.btn=sign in 2 | login.password=password 3 | login.remember=remember 4 | login.tip=Please sign in 5 | login.username=username -------------------------------------------------------------------------------- /demo/staff/src/main/resources/i18n/login_zh_CN.properties: -------------------------------------------------------------------------------- 1 | login.btn=登录 2 | login.password=密码 3 | login.remember=记住我 4 | login.tip=请登录 5 | login.username=用户名 -------------------------------------------------------------------------------- /demo/staff/src/main/resources/static/css/dashboard.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: .875rem; 3 | } 4 | 5 | .feather { 6 | width: 16px; 7 | height: 16px; 8 | vertical-align: text-bottom; 9 | } 10 | 11 | /* 12 | * Sidebar 13 | */ 14 | 15 | .sidebar { 16 | position: fixed; 17 | top: 0; 18 | bottom: 0; 19 | left: 0; 20 | z-index: 100; /* Behind the navbar */ 21 | padding: 0; 22 | box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1); 23 | } 24 | 25 | .sidebar-sticky { 26 | position: -webkit-sticky; 27 | position: sticky; 28 | top: 48px; /* Height of navbar */ 29 | height: calc(100vh - 48px); 30 | padding-top: .5rem; 31 | overflow-x: hidden; 32 | overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */ 33 | } 34 | 35 | .sidebar .nav-link { 36 | font-weight: 500; 37 | color: #333; 38 | } 39 | 40 | .sidebar .nav-link .feather { 41 | margin-right: 4px; 42 | color: #999; 43 | } 44 | 45 | .sidebar .nav-link.active { 46 | color: #007bff; 47 | } 48 | 49 | .sidebar .nav-link:hover .feather, 50 | .sidebar .nav-link.active .feather { 51 | color: inherit; 52 | } 53 | 54 | .sidebar-heading { 55 | font-size: .75rem; 56 | text-transform: uppercase; 57 | } 58 | 59 | /* 60 | * Navbar 61 | */ 62 | 63 | .navbar-brand { 64 | padding-top: .75rem; 65 | padding-bottom: .75rem; 66 | font-size: 1rem; 67 | background-color: rgba(0, 0, 0, .25); 68 | box-shadow: inset -1px 0 0 rgba(0, 0, 0, .25); 69 | } 70 | 71 | .navbar .form-control { 72 | padding: .75rem 1rem; 73 | border-width: 0; 74 | border-radius: 0; 75 | } 76 | 77 | .form-control-dark { 78 | color: #fff; 79 | background-color: rgba(255, 255, 255, .1); 80 | border-color: rgba(255, 255, 255, .1); 81 | } 82 | 83 | .form-control-dark:focus { 84 | border-color: transparent; 85 | box-shadow: 0 0 0 3px rgba(255, 255, 255, .25); 86 | } 87 | 88 | /* 89 | * Utilities 90 | */ 91 | 92 | .border-top { border-top: 1px solid #e5e5e5; } 93 | .border-bottom { border-bottom: 1px solid #e5e5e5; } 94 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/static/css/signin.css: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | height: 100%; 4 | } 5 | 6 | body { 7 | display: -ms-flexbox; 8 | display: -webkit-box; 9 | display: flex; 10 | -ms-flex-align: center; 11 | -ms-flex-pack: center; 12 | -webkit-box-align: center; 13 | align-items: center; 14 | -webkit-box-pack: center; 15 | justify-content: center; 16 | padding-top: 40px; 17 | padding-bottom: 40px; 18 | /*background-color: #f5f5f5;*/ 19 | } 20 | 21 | .form-signin { 22 | width: 100%; 23 | max-width: 330px; 24 | padding: 15px; 25 | margin: 0 auto; 26 | } 27 | .form-signin .checkbox { 28 | font-weight: 400; 29 | } 30 | .form-signin .form-control { 31 | position: relative; 32 | box-sizing: border-box; 33 | height: auto; 34 | padding: 10px; 35 | font-size: 16px; 36 | } 37 | .form-signin .form-control:focus { 38 | z-index: 2; 39 | } 40 | .form-signin input[type="email"] { 41 | margin-bottom: -1px; 42 | border-bottom-right-radius: 0; 43 | border-bottom-left-radius: 0; 44 | } 45 | .form-signin input[type="password"] { 46 | margin-bottom: 10px; 47 | border-top-left-radius: 0; 48 | border-top-right-radius: 0; 49 | } 50 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/static/img/bootstrap-solid.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/templates/commons/commons.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 14 | 15 | 131 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/templates/dashboard.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Dashboard Template for Bootstrap 11 | 12 | 13 | 14 | 15 | 16 | 42 | 43 | 44 | 45 |
46 |
47 |
48 |
49 | 50 |
51 | 59 |
60 |

Dashboard

61 |
62 |
63 | 64 | 65 |
66 | 70 |
71 |
72 | 73 | 74 | 75 | 76 |
77 |
78 |
79 | 80 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 92 | 93 | 94 | 95 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/templates/emp/add.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Dashboard Template for Bootstrap 12 | 13 | 14 | 15 | 16 | 17 | 43 | 44 | 45 | 46 |
47 | 48 |
49 |
50 |
51 | 52 |
53 |

添加员工

54 |
55 |
56 | 57 | 58 |
59 |
60 | 61 | 62 |
63 |
64 |
65 |
66 | 67 | 68 |
69 |
70 | 71 | 72 |
73 |
74 |
75 | 76 | 80 |
81 |
82 | 83 | 84 |
85 | 86 |
87 |
88 |
89 |
90 | 91 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 103 | 104 | 105 | 106 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/templates/emp/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Dashboard Template for Bootstrap 12 | 13 | 14 | 15 | 16 | 17 | 43 | 44 | 45 | 46 |
47 | 48 |
49 |
50 |
51 | 52 |
53 |

添加员工

54 |
55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 79 | 80 | 81 |
idlastNameemailgenderdepartmentbirth操作
76 | 编辑 77 | 删除 78 |
82 |
83 |
84 |
85 |
86 | 87 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 99 | 100 | 101 | 102 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/templates/emp/update.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Dashboard Template for Bootstrap 12 | 13 | 14 | 15 | 16 | 17 | 43 | 44 | 45 | 46 |
47 | 48 |
49 |
50 |
51 | 52 |
53 | 54 | 55 |
56 | 57 | 58 |
59 | 60 | 61 |
62 |
63 | 64 | 65 |
66 |
67 |
68 |
69 | 70 | 71 |
72 |
73 | 74 | 75 |
76 |
77 |
78 | 79 | 84 |
85 |
86 | 87 | 88 |
89 | 90 |
91 |
92 |
93 |
94 | 95 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 107 | 108 | 109 | 110 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/templates/error/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Dashboard Template for Bootstrap 11 | 12 | 13 | 14 | 15 | 16 | 42 | 43 | 44 | 45 | 54 | 55 | 185 | 186 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 198 | 199 | 200 | 201 | 230 | 231 | 232 | 233 | -------------------------------------------------------------------------------- /demo/staff/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Signin Template for Bootstrap 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /demo/staff/src/test/java/com/mine/StaffApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.mine; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class StaffApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /note/SSM的整合.md: -------------------------------------------------------------------------------- 1 | 思路主要是先搭建Spring的配置环境,然后在通过spring整合SpringMVC框架,最后再整合MyBatis. 文中完整的demo点我(包含整合必要的jar和xml配置等) 2 | 3 | ##### 搭建Spring的开发环境 4 | 1 . 创建个基于maven的web骨架工程. 5 | 2. 创建一个source Root的java文件夹和resources Root的resources文件夹. 6 | 3. 在pom.xml中引入工程必要的jar包.jar包的完整坐标可参考文首demo中的pom.xml文件.( 7 | 4. 编写dao,domain以及service层. 8 | 5. 搭建和测试Spring的开发环境. 9 | 在resources下新建一个applicationContext.xml的配置文件,编写具体的配置信息. 10 | 重点是在配置注解扫描时, 由于controller部分是基于springMVC的框架来管理的,所以要注意忽略掉web层的注解.同理在springMVC的注解扫描配置时依然需要注意只扫描web层. 11 | 12 | ```java 13 | 14 | 27 | // 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架 去管理 28 | 29 | // 配置要忽略的注解 30 | 32 | 33 | 34 | 35 | 36 | ``` 37 | 38 | ##### Spring整合SpringMVC框架 39 | 1. 在web.xml中配置DispatcherServlet前端控制器. 40 | 41 | ```java 42 | // 配置前端控制器:服务器启动必须加载,需要加载springmvc.xml配置文件 43 | 44 | dispatcherServlet org.springframework.web.servlet.DispatcherServlet 45 | // 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 46 | 47 | contextConfigLocation 48 | classpath:springmvc.xml 49 | 50 | // 服务器启动的时候,让DispatcherServlet对象创建 51 | 1 52 | 53 | 54 | dispatcherServlet 55 | / 56 | 57 | 58 | ``` 59 | 2. 创建springmvc.xml的配置文件,编写配置文件. 60 | 61 | ```java 62 | 63 | 74 | // 扫描controller的注解,别的不扫描 75 | 76 | 78 | 79 | // 配置视图解析器 80 | 81 | // JSP文件所在的目录 82 | 83 | // 文件的后缀名 84 | 85 | 86 | // 设置静态资源不过滤 87 | 88 | // 开启对SpringMVC注解的支持 89 | 90 | 91 | 92 | ``` 93 | 94 | 3. spring整合springMVC的框架 95 | 整合的目的主要是可以成功在controller中调用service层的方法, 只需要在项目启动的时候去加载spring的配置文件applicationContext.xml, 可以通过监听器监听servletContext的创建和销毁来实现.在web.xml中配置ContextLoaderListener监听器, 默认只会加载WEB-INF目录下的applicationContext.xml配置文件,因此需要设置applicationContext.xml的路径. 96 | 97 | ```java 98 | 99 | org.springframework.web.context.ContextLoaderListener 100 | 101 | // 设置applicationContext.xml的路径.便于监听器加载 102 | 103 | contextConfigLocation 104 | classpath:applicationContext.xml 105 | 106 | 107 | ``` 108 | 至此spring整合springMVC完成, 可以在web层的controller中通过注解注入的方式获取service容器. web层可以成功调用service层业务接口. 例如: 109 | 110 | ```java 111 | // web 112 | @Controller 113 | @RequestMapping("/account") 114 | public class AccountController { 115 | @Autowired 116 | private AccountService accountService; 117 | @RequestMapping("/findAll") 118 | public String findAll() { 119 | System.out.println("表现层方法调用"); 120 | accountService.findAll(); 121 | accountService.savaAccount(new Account()); 122 | return "success"; 123 | } 124 | } 125 | 126 | ``` 127 | 128 | ##### Spring整合MyBatis框架 129 | 1. 在resources编写MapConfig.xml的配置文件, 编写核心配置文件. 130 | 2. 在AccountDao接口的方法上通过注解的方式编写sql语句. 131 | 3. 编写测试的方法. 132 | 4. 在applicationConfig中配置mybatis相关内容,连接池,sqlsession工厂等.也就是把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中. 133 | 134 | ```java 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | ``` 149 | 5. 在applicationConfig完善声明式事务管理. 150 | 至此, SSM整合完成. 151 | 在整合的过程,一定要注意jar的版本问题,否则,极易引起一些未知错误. -------------------------------------------------------------------------------- /note/Spring基于xml的Ioc实现.md: -------------------------------------------------------------------------------- 1 | Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架.而IOC的核心就是解除代码间的依赖关系,削减程序间的耦合.Spring中的解耦思想从应用层面来说主要体现在两个方面:①使用反射来创建对象, 而避免使用new关键字;②通过读取配置文件来获取要创建的对象全限定类名. 2 | 3 | ##### 基本配置 4 | * 环境搭建 5 | 1. 通过maven创建一个项目.然后在项目的核心配置文件(pom.xml)中配置spring的包. 6 | 7 | ```java 8 | 9 | 10 | org.springframework 11 | spring-context 12 | 5.1.5.RELEASE 13 | 14 | 15 | ``` 16 | 2 . 在配置文件完善配置信息 17 | ---- bean标签:用于配置让spring创建对象, 并且存入ioc容器之中. 18 | ---- id 属性:对象的唯一标识. 19 | ---- class 属性:指定要创建对象的全限定类名. 20 | ```java 21 | 22 | 26 | 27 | 28 | 29 | 30 | ``` 31 | 3. 测试配置结果 32 | ```ApplicationContext 接口的实现类:``` 33 | 1)ClassPathXmlApplicationContext: 34 | 它是从类的根路径下加载配置文件.(推荐) 35 | 2)FileSystemXmlApplicationContext: 36 | 它是从磁盘路径上加载配置文件, 配置文件可以在磁盘的任意位置. 37 | 3)AnnotationConfigApplicationContext: 38 | 当我们使用注解配置容器对象时,需要使用此类来创建 spring 容器,它用来读取注解. 39 | ```java 40 | import org.springframework.context.support.ClassPathXmlApplicationContext; 41 | 42 | public class SpringDemo { 43 | public static void main(String[] args) { 44 | // 通过ApplicationContext接口来获取spring容器 45 | ClassPathXmlApplicationContext path = new ClassPathXmlApplicationContext("bean.xml"); 46 | // 通过bean的id获取对象 47 | Object accountService = path.getBean("accountService"); 48 | System.out.println(accountService); 49 | } 50 | } 51 | ``` 52 | 53 | ##### Bean 标签 54 | bean标签的主要作用是配置对象让 spring 来创建。默认情况下它调用的是类中的无参构造函数, 如果没有无参构造函数则不能创建成功. 55 | * 属性 56 | ```id:```给对象在容器中提供一个唯一标识.用于获取对象. 57 | ```class:```指定类的全限定类名, 用于反射创建对象。默认情况下调用无参构造函数. 58 | ```scope:```指定对象的作用范围. 59 | scope属性取值主要包括如下: 60 | ```singleton :```默认值,单例的. 61 | ```prototype :```多例的. 62 | ```request :```WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中. * session :WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中. * ```global session :```WEB项目中,应用在Portlet环境.如果没有Portlet环境那么相globalSession 相当于 session. 63 | ```init-method:```指定类中的初始化方法名称. 64 | ```destroy-method:```指定类中销毁方法名称. 65 | * bean 生命周期 66 | ```单例对象:scope="singleton" ```一个应用只有一个对象的实例,它的作用范围就是整个应用. 67 | 1)生命周期: 68 | 对象出生:当应用加载,创建容器时, 对象就被创建了. 69 | 对象活着:只要容器在, 对象一直活着. 70 | 对象死亡:当应用卸载, 销毁容器时, 对象就被销毁了. 71 | 2)```多例对象:scope="prototype"```每次访问对象时, 都会重新创建对象实例. 72 | 生命周期: 73 | 对象出生:当使用对象时, 创建新的对象实例. 74 | 对象活着:只要对象在使用中, 就一直活着. 75 | 对象死亡:当对象长时间不用时, 被 java 的垃圾回收器回收了. 76 | * Bean 的实例化 77 | ```方式一: 使用默认无参构造函数``` 78 | 这种场景下, 它会根据默认无参构造函数来创建类对象. 如果 bean 中没有默认无参构造函数, 将会创建失败. 79 | 80 | ```java 81 | 82 | ``` 83 | ```方式二: 使用静态工厂的方法创建对象``` 84 | 静态工厂方法创建对象时, xml中的bean对象配置: 85 | ```id 属性:```指定 bean 的 id,用于从容器中获取 86 | ```class 属性:```指定静态工厂的全限定类名 87 | ```factory-method 属性:```指定生产对象的静态方法 88 | ```java 89 | // 提供一个静态工厂方法 90 | public class StaticFactory { 91 | public static IAccountSave createAccountService() { 92 | return new AccountImpl(); 93 | } 94 | } 95 | 96 | // 配置bean 97 | 100 | 101 | 其中: 102 | 使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器. 103 | 104 | ``` 105 | ```方式三: 使用实例工厂的方法来创建对象``` 106 | 107 | ```java 108 | // 提供一个实例工厂方法 109 | public class InstanceFactory { 110 | public IAccountService createAccountService(){ 111 | return new AccountServiceImpl(); } 112 | } 113 | // 配置bean.xml 114 | 115 | 119 | 120 | 121 | 124 | 125 | 126 | ``` 127 | 128 | ##### spring 的依赖注入 129 | * 第一种: 使用构造函数注入 130 | 使用构造函数这种注入方式需要为当前类提供一个与参数列表相对应的构造函数. 131 | 构造函数注入方式的bean配置标签为```constructor-arg```: 132 | 主要属性: 133 | ```index:```指定参数在构造函数参数列表的索引位置 134 | ```type:```指定参数在构造函数中的数据类型 135 | ```name:```指定参数在构造函数中的名称<推荐使用> 136 | ```value:```它能赋的值是基本数据类型和 String 类型 137 | ```ref:```它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean.例如下例中的date属性. 138 | ```java 139 | eg: 140 | // 提供一个构造函数 141 | public class AccountDaoImpl implements IAccountDao { 142 | private int age; 143 | private String name; 144 | private Date birthday; 145 | 146 | public AccountDaoImpl(int age, String name, Date birthday) { 147 | this.age = age; 148 | this.name = name; 149 | this.birthday = birthday; 150 | } 151 | 152 | @Override 153 | public String toString() { 154 | return "AccountDaoImpl{" + 155 | "age=" + age + 156 | ", name='" + name + '\'' + 157 | ", birthday=" + birthday + 158 | '}'; 159 | } 160 | } 161 | 162 | // 配置bean.xml文件 163 | 164 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | ``` 178 | * 第二种: 使用set方法注入 <推荐使用> 179 | set方法注入需要在类中提供对应属性的set方法. 180 | set方法注入其bean配置的标签为:```property``` 181 | 主要属性有: 182 | ```name:```找的是类中 set 方法后面的部分 183 | ```ref:```给属性赋值是其他 bean 类型的 184 | ```value:```给属性赋值是基本数据类型和 string 类型的 185 | 186 | ```java 187 | 188 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | ``` 201 | 构造函数注入,在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功.这种方式的弊端主要是,它颠覆了bean对象的实例化方式,使我们在创建对象时, 即使可能用不到某些数据, 但依然需要提供. 202 | * 复杂集合类型的注入 203 | 在注入集合数据时,只要结构相同,标签可以互换. 如List 结构的: array,list,set 204 | Map 结构的 map,entry,props,prop等标签均可互换. 205 | ```java 206 | 207 | 211 | 212 | 213 | 214 | 215 | 李白 216 | 阿珂 217 | 孙尚香 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | ``` -------------------------------------------------------------------------------- /note/Spring基于注解的Ioc实现.md: -------------------------------------------------------------------------------- 1 | 2 | #### Spring使用之基于注解的Ioc配置 3 | 4 | 5 | 6 | Spring的注解需要在配置文件中指定需要扫描的包.具体配置参照: 7 | 8 | ```java 9 | 10 | 17 | 18 | 19 | 20 | 21 | 22 | ``` 23 | 24 | ##### 常用注解 25 | * @Component 26 | @Component作用是把资源让 spring 来管理. 相当于在 xml 中配置一个 bean. 它有一个value属性, 通常用来指定 bean 的 id. 如果不指定 value 属性,默认 bean 的 id 是当前类的类名, 首字母小写. 27 | 28 | ```java 29 | // 在需要创建对象的类中添加注解关键字@Component 30 | @Component 31 | public class AccountServiceImpl implements IAccountService { 32 | public AccountServiceImpl () { 33 | System.out.println("创建了对象"); 34 | } 35 | } 36 | // 在测试文件中, 如果@Component注解未指定value值,则默认id为当前类名,首字母小写.即下面的"accountServiceImpl". 37 | public class Test { 38 | public static void main(String[] args) { 39 | ClassPathXmlApplicationContext cls = new ClassPathXmlApplicationContext("bean.xml"); 40 | IAccountService accountService = (IAccountService) cls.getBean("accountServiceImpl"); 41 | System.out.println(accountService); 42 | } 43 | } 44 | 45 | ``` 46 | * @Controller @Service @Repository 47 | 这三个注解的作用跟@Component 一模一样,区别主要在于:Controller: 一般用于表现层的注解; @Service: 一般用于业务层的注解; @Repository: 一般用于持久层的注解. 48 | * @Autowired 49 | @Autowired会自动按照类型注入. 当使用注解注入属性时, set 方法可以省略. 只要容器中有唯一一个bean对象类型和要注入的变量类型匹配, 就可以注入成功. 它只能注入其他 bean 类型, 当有多个 类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功,找不到 就报错.@Autowired注解的位置大多用在变量和方法上,当然不限于此. 50 | 51 | ```java 52 | @Service("accountService") 53 | public class AccountServiceImpl implements IAccountService { 54 | @Autowired 55 | private IAccountDao dao; 56 | public void saveAccout() { 57 | dao.saveAccount(); 58 | } 59 | } 60 | 61 | ``` 62 | * @Qualifier 63 | 在自动按照类型注入的基础之上,再按照名称(Bean的id )注入。它在给类成员注入时不能独立使用,必须和@Autowire 一起使用; 但是给方法参数注入时,可以独立使用. 64 | 属性: 有一个属性value,用于指定注入的bean 的 id. 65 | 在给类成员注入时,必须要和@Autowired组合使用. 66 | * @Resource 67 | 直接按照 Bean的 id 注入, 可以独立使用. 它也只能注入其他 bean 类型. 68 | * @Value 69 | 用来注入基本数据类型和 String 类型数据的.它有一个value属性,用来指定数据的值,它可以使用spring中的SpEL表达式. 70 | * 说明: @Autowired,@Qualifier和@Resource三个都只能注入其他的bean类型的数据,而基本数据类型和string类型只能通过@Value来实现. 71 | * @Scope 72 | 用来指定bean的作用范围.有一个value属性值用来指定范围的值。 73 | 取值包括:singleton prototype request session globalsession 74 | 75 | ##### 新注解 76 | * @ComponentScan 77 | 用于指定 spring 在初始化容器时要扫描的包.作用和在 spring 的 xml 配置文件中的:是一样的.其中的basePackages属性用于指定要扫描的包,和该注解中的 value 属性作用一样. 78 | * @Bean 79 | Bean注解只能写在方法上, 表明使用此方法创建一个对象,并且放入 spring 容器. 80 | 属性name表示给当前@Bean 注解方法创建的对象指定一个名称(即 bean 的 id). 81 | 实例:需要创建一个SpringConfiguration的类来配置注解相关信息 82 | * @Import 83 | 用于导入其他配置类, 在引入其他配置类时, 可以不用再写@Configuration 注解, 当然,写上也不影响. 84 | * @PropertySource 85 | 用于加载.properties 文件中的配置. 例如我们配置数据源时,可以把连接数据库的信息写到 properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置. 属性value[]:用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath. 86 | 如: 87 | 存在一个jdbcConfig.properties的配置文件 88 | 89 | ```java 90 | jdbc.driver=com.mysql.cj.jdbc.Driver 91 | jdbc.url=jdbc:mysql://localhost:3306/jdbcDemo 92 | jdbc.user=root 93 | jdbc.password=12345678 94 | ``` 95 | 在配置文件中: 96 | ```java 97 | @Configuration 98 | @ComponentScan("com.mine") 99 | @PropertySource("jdbcConfig.properties") 100 | public class SpringConfiguration { 101 | @Value("${jdbc.driver}") 102 | private String driver; 103 | 104 | @Value("${jdbc.url}") 105 | private String url; 106 | 107 | @Value("${jdbc.user}") 108 | private String user; 109 | 110 | @Value("${jdbc.password}") 111 | private String password; 112 | 113 | /** 114 | * 创建一个QueryRunner对象 115 | * @param dataSource 116 | * @return 117 | */ 118 | @Bean(name = "runner") 119 | @Scope("prototype") 120 | public QueryRunner createQueryRunner (DataSource dataSource) { 121 | return new QueryRunner(dataSource); 122 | } 123 | 124 | @Bean(name ="dataSource") 125 | public DataSource createDataSource() { 126 | try { 127 | ComboPooledDataSource dataSource = new ComboPooledDataSource(); 128 | dataSource.setDriverClass(driver); 129 | dataSource.setJdbcUrl(url); 130 | dataSource.setUser(user); 131 | dataSource.setPassword(password); 132 | return dataSource; 133 | } catch (Exception e){ 134 | throw new RuntimeException(); 135 | } 136 | } 137 | } 138 | 139 | ``` 140 | 141 | 同时在测试模块需要修改读取注解的方法: 142 | 143 | ```java 144 | ApplicationContext cls = new AnnotationConfigApplicationContext(SpringConfiguration.class); 145 | 146 | ``` 147 | 148 | ##### Junit的集成 149 | * 分析 150 | 在测试类中,每个测试方法都有以下两行代码: 151 | 152 | ```java 153 | ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 154 | IAccountService as = ac.getBean("accountService",IAccountService.class); 155 | 156 | ``` 157 | 这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常.所以又不能轻易删掉. 158 | * 为什么不把测试类配到 xml 中? 159 | 配置到xml当然可以正常使用, 只是因为: 160 | ①当我们在 xml 中配置了一个 bean,spring 加载配置文件创建容器时,就会创建对象. 161 | ②测试类只是我们在测试功能时使用,而在项目中它并不参与程序逻辑,也不会解决需求上的问题,所以创建完了,并没有使用. 那么存在容器中就会造成资源的浪费. 所以,我们不应该把测试配置到 xml 文件中. 162 | 163 | * 集成 164 | 1) 导入spring整合junit的jar包(坐标) 165 | 166 | ```java 167 | 168 | org.springframework 169 | spring-test 170 | 5.0.7.RELEASE 171 | 172 | ``` 173 | 2)使用junit提供的一个main方法把原有的main方法替换掉, 替换成spring提供的@Runwith. 174 | 175 | ```java 176 | @RunWith(SpringJUnit4ClassRunner.class) 177 | ``` 178 | 之所以需要替换,是以为junit单元测试中, 没有main方法也能执行, junit本身集成了一个main方法, 该方法会判断当前测试类中哪些方法包含@Test的注解,junit就会让这些带有@Test的方法执行. 而junit本身并不会关注我们的程序是否集成了spring,所以在执行测试方法时候, 它也不会为我们读取配置文件或配置类来为我们创建容器.查看源码可知, SpringJUnit4ClassRunner其本身也是继承了junit. 虽然junit不会为我们创建容器,由于SpringJUnit4ClassRunner是spring提供,所以它一定会为我们创建容器. 179 | 180 | 3)告知spring的运行期,spring的Ioc创建是基于xml还是注解.并且通过@ContextConfiguration 注解来说明位置. 181 | @ContextConfiguration 注解: 182 | 其中, 183 | locations 属性用于指定配置文件的位置. 如果是类路径下,需要用 classpath表明. 184 | 185 | ```java 186 | @RunWith(SpringJUnit4ClassRunner.class) 187 | @ContextConfiguration(locations= {"classpath:bean.xml"}) 188 | public class AccountServiceTest { 189 | } 190 | 191 | ``` 192 | classes 属性用于指定注解的类, 当不使用 xml 配置时,需要用此属性指定注解类的位置. 193 | 194 | ```java 195 | @RunWith(SpringJUnit4ClassRunner.class) 196 | @ContextConfiguration(classes = SpringConfiguration.class) 197 | public class test { 198 | 199 | } 200 | ``` 201 | 4)通过以上三步的配置, 在后续的单元测试中, 可以不需要在重复写创建容器的代码,简单配置即可.实例如下: 202 | 203 | ```java 204 | // 替换原有的main方法 205 | @RunWith(SpringJUnit4ClassRunner.class) 206 | // 指定注解类的位置 207 | @ContextConfiguration(classes = SpringConfiguration.class) 208 | public class test { 209 | // 注入 210 | @Autowired 211 | private IAccountService accountService; 212 | 213 | @Test 214 | public void findAllAccount() { 215 | List allAccount = accountService.findAllAccount(); 216 | for (Account account : allAccount) { 217 | System.out.println(account); 218 | } 219 | } 220 | } 221 | 222 | 223 | ``` 224 | 当使用spring 5.x版本时候, 需要junit的包必须是4.12以上,否则会报错. 225 | -------------------------------------------------------------------------------- /note/mybatis使用教程.md: -------------------------------------------------------------------------------- 1 | #### 基本环境搭建 2 | 1 新建一个无骨架的maven工程, 要在maven的directory中指定maven的安装路径和setting.xml,responstory的位置. 3 | 2 删除新建工程src目录,让其成为一个父类,充当父工程的角色.这样设计的目的是不需要在重复的为后来新建的模块设置对应的jar依赖. 4 | 3 在pom.xml中导入常用的jar包,如mysql, mybatis,junit等. 5 | 6 | ```java 7 | 8 | 11 | 4.0.0 12 | 13 | 14 | cn.mineTest 15 | mybatis_01 16 | pom 17 | 1.0-SNAPSHOT 18 | 19 | mybatis_study_01 20 | 21 | 22 | 23 | 24 | mysql 25 | mysql-connector-java 26 | 5.1.46 27 | 28 | 29 | junit 30 | junit 31 | 4.12 32 | 33 | 34 | org.mybatis 35 | mybatis 36 | 3.5.2 37 | 38 | 39 | 40 | 41 | 42 | 43 | src/main/resources 44 | 45 | **/*.properties 46 | **/*.xml 47 | 48 | true 49 | 50 | 51 | src/main/java 52 | 53 | **/*.properties 54 | **/*.xml 55 | 56 | true 57 | 58 | 59 | 60 | 61 | 62 | ``` 63 | 64 | 4 创建一个模块作为我们的工程项目,观察父包中多了一个新建的这个模块.新建的模块不需要在重新导包了.这就是maven父子继承的好处.(父工程的核心配置中会有子工程的mudule,表示子工程也拥有跟父类相同的配置) 65 | 66 | ```java 67 | 68 | mybatis_study_01 69 | 70 | ``` 71 | 72 | #### mybatis的集成 73 | 74 | 1 直接在mybatis搜索即可 75 | 76 | ```java 77 | 78 | org.mybatis 79 | mybatis 80 | 3.5.2 81 | 82 | ``` 83 | 84 | 2. 参照mybatis文档,封装加载核心配置文件的工具类.例如: 85 | 86 | ```java 87 | package com.mine.utils; 88 | 89 | import org.apache.ibatis.io.Resources; 90 | import org.apache.ibatis.session.SqlSession; 91 | import org.apache.ibatis.session.SqlSessionFactory; 92 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 93 | import java.io.IOException; 94 | import java.io.InputStream; 95 | 96 | // sqlSessionFactory工具类 97 | public class MybatisUtils { 98 | private static SqlSessionFactory sqlSessionFactory; 99 | static { 100 | try { 101 | String resource = "mybatis_config.xml"; 102 | InputStream inputStream = Resources.getResourceAsStream(resource); 103 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 104 | } catch (IOException e) { 105 | e.printStackTrace(); 106 | } 107 | } 108 | 109 | // 获取可以执行sql的sqlSession对象 110 | public static SqlSession getSqlSession() { 111 | return sqlSessionFactory.openSession(); 112 | } 113 | } 114 | ``` 115 | 116 | 常见错误: 117 | * 配置文件没有注册,错误提示如下: 118 | 119 | ```java 120 | org.apache.ibatis.binding.BindingException: Type interface 121 | com.mine.dao.UserDao is not known to the MapperRegistry. 122 | ``` 123 | 124 | 解决方法:每一个Mapper.xml都需要在mybatis的核心配置文件中注册. 125 | 126 | ```java 127 | 例如:在mybatis核心配置中注册UserMapper.xml. 128 | 129 | 130 | 131 | ``` 132 | 133 | * 配置文件无法导入或生效的问题.即resouces中的配置文件导出失败.解决方案是在build中配置resource. 134 | 135 | ```java 136 | 137 | 138 | 139 | src/main/resources 140 | 141 | **/*.properties 142 | **/*.xml 143 | 144 | true 145 | 146 | 147 | src/main/java 148 | 149 | **/*.properties 150 | **/*.xml 151 | 152 | true 153 | 154 | 155 | 156 | ``` 157 | 158 | #### CRUD操作 159 | 160 | * namespace中的包名要和Dao/Mapper中的接口的包名保持一致. 161 | * select选择查询语句 162 | * id: 指的就是namespace中的方法名. 163 | * resultType: sql语句执行的返回值. 164 | * parameterType: 参数类型. 165 | 166 | 注意点: 增删改需要提交事务. 167 | 例如: 168 | 169 | ```java 170 | // UserMapper 171 | public interface UserMapper { 172 | List getUserList(); 173 | User getUserById(int id); 174 | } 175 | // UserMapper.xml 176 | 177 | 180 | 181 | 184 | // 185 | 186 | insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd}) 187 | 188 | 189 | 190 | update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id} 191 | 192 | 193 | 194 | delete from mybatis.user where id = #{id} 195 | 196 | 197 | 198 | 199 | ``` 200 | 201 | #### 配置解析优化 202 | 203 | ###### 环境配置(environments) 204 | 尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境.可以通过默认使用的环境 ID(default="development")属性来切换配置环境。 205 | 206 | ###### 属性(properties) 207 | 可以通过properties属性来实现引用配置文件,这些属性都是可以外部配置且动态替换,既可以在典型的java属性文件中配置,亦可以通过properties元素的子元素来传递. 208 | 例如: 209 | (1)我们可以在resources下新建一个db. properties文件,在其中配置db相关的设置. 210 | 211 | ```java 212 | driver=com.mysql.jdbc.Driver 213 | url=jdbc:mysql://localhost:3306/mybatis?useSSL=false 214 | username=root 215 | password=12345678 216 | ``` 217 | 218 | (2)然后在核心配置文件中通过properties标签元素引入db.proerties文件即可. 219 | 220 | ```java 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | ``` 241 | 242 | * 关于引入外部文件的注意事项: 243 | (1)可以直接引入 244 | (2)可以在其中增加一些属性配置 245 | (3)如果两个文件同一个字段,则会优先使用外部配置文件的.比如我们在db. properties中配置了账户密码,在核心配置文件中引入db. properties之后,在properties标签中也配置了账户密码,那么此时会优先使用外部配置文件db. properties中的账户密码. 246 | #####类型别名(typeAliases) 247 | 类型别名目的是为Java类型设置一个短的名字,它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余. 248 | * 给类型取别名有两种方式: 249 | (1)直接使用typeAlias标签 250 | 251 | ```java 252 | 253 | 254 | 255 | 256 | ``` 257 | 258 | (2)指定一个包名 259 | 指定包名这种方式,Mybatis会在包名下面搜索需要的JavaBean.这种设置,会扫描实体类的包,则它的默认别名就是当前这个类的类名,首字母小写. 260 | 261 | ```java 262 | 263 | 264 | 265 | ``` 266 | 267 | 两种取别名的方式区别在于第一种可以自定义别名,第二种则不行.对于第二种方法若一定要自定义别名,而不采用类首字母小写的方式作为别名,则需要在对应的实体类上增加注解的方式可以实现. 268 | 269 | ```java 270 | 例如: 271 | @Alias("hello") 272 | public class User { 273 | ``` 274 | 275 | #### 映射器(Mapper) 276 | 277 | MapperRegistry主要是帮助我们注册绑定Mapper文件.有三种实现方式. 278 | (1) 直接使用resource来指定对应文件. 279 | 280 | ```java 281 | 282 | 283 | ``` 284 | 285 | (2)使用class文件帮到注册 286 | 287 | ```java 288 | 289 | 290 | ``` 291 | 292 | (3)使用扫描包进行注入绑定 293 | 294 | ```java 295 | 296 | 297 | 298 | ``` 299 | 300 | 其中方法(2)和(3)中需要尤其注意: 301 | 接口和Mapper配置文件必须同名 302 | 接口和Mapper配置文件必须在同一个包下. 303 | #### 日志 304 | 如果数据库操作,出现异常,则需要排错,日志就是最好的帮手. 305 | 日志工厂:logImpl 306 | (1)STDOUT_LOGGING为标准日志工厂实现. 307 | * 在mybatis的核心配置文件中,配置我们的日志 308 | 309 | ```java 310 | 311 | 312 | 313 | 314 | ``` 315 | 316 | (2)LOG4J 317 | * 可以控制日志信息输送的目的地是控制台、文件、GUI组件等 318 | * 可以控制每一条日志的输出格式 319 | * 可以定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程 320 | * 可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。 321 | log4j的配置: 322 | (1)先导入log4j依赖包. 323 | (2)log4j.properties配置 324 | 325 | ```java 326 | ### 配置根 ### 327 | log4j.rootLogger = DEBUG,console ,file 328 | 329 | ### 配置输出到控制台 ### 330 | log4j.appender.console = org.apache.log4j.ConsoleAppender 331 | log4j.appender.console.Target = System.out 332 | log4j.appender.console.Threshold = DEBUG 333 | log4j.appender.console.layout = org.apache.log4j.PatternLayout 334 | log4j.appender.console.layout.ConversionPattern = [%c] - %m%n 335 | 336 | ### 配置输出到文件 ### 337 | log4j.appender.file = org.apache.log4j.RollingFileAppender 338 | log4j.appender.file.File = ./logs/log4jTest.log 339 | log4j.appender.file.maxFileSize = 10mb 340 | #log4j.appender.fileAppender.Append = true 341 | log4j.appender.file.Threshold = DEBUG 342 | log4j.appender.file.layout = org.apache.log4j.PatternLayout 343 | log4j.appender.file.layout.ConversionPattern = [%p][%d{yy-MM-dd}][%c]%m%n 344 | 345 | ### 设置输出sql的级别,其中logger后面的内容全部为jar包中所包含的包名 ### 346 | log4j.logger.org.apache = DEBUG 347 | log4j.logger.org.mybatis = DEBUG 348 | log4j.logger.java.sql.Connection = DEBUG 349 | log4j.logger.java.sql.Statement = DEBUG 350 | log4j.logger.java.sql.PreparedStatement = DEBUG 351 | log4j.logger.java.sql.ResultSet = DEBUG 352 | 353 | ``` 354 | 355 | (3)在setting中配置log4j为日志的实现方式 356 | 357 | ```java 358 | 359 | 360 | 361 | ``` 362 | 363 | log4j使用: 364 | (1) 在要使用log4j的类中导入包 ```import org.apache.log4j.Logger;``` 365 | (2) 生成日志对象, 日志对象为当前类的class 366 | 367 | ```java 368 | @Test 369 | public void log4jTest() { 370 | Logger logger = Logger.getLogger(UserDaoTest.class); 371 | logger.info("log4j测试来了"); 372 | logger.debug("log4j的debug来了"); 373 | logger.error("log4j的error来了"); 374 | } 375 | 376 | ``` 377 | 378 | (3)日常开发中只需要在需要日志的地方补充上必要的logger.info语句即可快速定位. -------------------------------------------------------------------------------- /note/springBoot工程部署与集成.md: -------------------------------------------------------------------------------- 1 | springBoot主要是提供了一种款速使用spring的方式,没有代码生成,也无需配置xml.其核心功能提现在起步依赖和自动装配.起步依赖指的是将具备某种功能的坐标打包到一起,并提供一些默认的功能。自动装配则提现在Spring Boot是一个运行时的过程,对于spring的配置应该用哪个,不需要配置哪个,该过程是Spring自动完成的。 2 | 3 | ##### springBoot开发环境部署 4 | * 使用idea创建一个基础项目,不携带骨架式的. 5 | 1. 使用idea工具创建一个普通的maven工程.(无需携带骨架) 6 | 2. 添加SpringBoot的起步依赖. 7 | 1)springBoot的项目必须要继承springBoot的起步依赖spring-boot-starter-parent. 8 | 即: 9 | ```java 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | ``` 16 | 2)同时,springBoot要集成springMVC进行Controller的开发,也必须要导入web的启动依赖. 17 | 18 | ```java 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | ``` 27 | 3. 编写springBoot引导类. 28 | 要通过springBoot提供的引导类起步,springBoot才可以进行访问. 29 | <```@SpringBootApplication```表示SpringBoot的引导类.> 30 | 31 | ```java 32 | // 用以声明当前类是一个springBoot的引导类 33 | @SpringBootApplication 34 | public class MySpringBootApplication { 35 | // main方法是程序的入口 36 | public static void main(String[] args) { 37 | // run表示运行springBoot的引导类,参数是引导类的字节码文件 38 | SpringApplication.run(MySpringBootApplication.class); 39 | } 40 | } 41 | 42 | ``` 43 | 4. 编写controller类 44 | 在springBoot引导类MySpringBootApplication的同级目录新建一个子包controller,编写控制器相关代码.尤其需要注意的是,新建的这个controller包, 一定要和引导类在同级目录下,否则识别不到. 45 | 46 | ```java 47 | @Controller 48 | public class HelloController { 49 | @RequestMapping("/hello") 50 | @ResponseBody 51 | public String helloBoot() { 52 | return "hello springBoot"; 53 | } 54 | } 55 | ``` 56 | * 使用idea的spring initalizr快速构建项目 57 | spring initalizr的这种创建方式默认已经为我们提供了web的起步依赖相关坐标. 58 | 1. 新创建一个新项目, 选择spring initalizr ,直接点next. 59 | 2. 填写项目信息,选择初始化的组件,填写项目路径.在构建工具选择页面选择想要配置的组件, 等待项目构建成功. 60 | 3. SpringBootApplication的同路径下新建一个包 controller,编码即可. 61 | 62 | 63 | ##### springBoot工程的热部署 64 | 编码过程中需要反复修改类、页面等资源,每次修改后都是需要重新启动才生效,而热部署刚好可以优化这一弊端, 使得资源修改后不需要再次重启即可生效. 65 | 1 . 导入依赖 66 | ```java 67 | 68 | org.springframework.boot 69 | spring-boot-devtools 70 | 71 | ``` 72 | 2 . 修改compiler项. 勾选. 73 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191106145247553.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTEwODA4Nw==,size_16,color_FFFFFF,t_70) 74 | 3 . 修改registry配置,勾选. 75 | win: Shift+Ctrl+Alt+/,搜索Registry; 76 | mac: command+shift+A ; 搜索Registry; 77 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191106150828123.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTEwODA4Nw==,size_16,color_FFFFFF,t_70) 78 | 79 | ##### springBoot配置文件 80 | * 关于yml文件 81 | springBoot的很多配置都有默认值,如果想要替换默认的配置就需要添加application.properties或者application.yml(application.yaml)进行配置, springBoot默认会从Resources目录下加载application.properties或application.yml(application.yaml)文件.application.properties里面的内容以```key: value```的形式配置.其中需要注意的是:```key: value```之间冒号与value之间必须要一个空格. 82 | application.yml文件实例: 83 | 84 | ```yaml 85 | # 关于格式: ①注意 "-"与后面值之间必须有一空格; ②":"与value之间的必须有一空格. 86 | # 普通数据 87 | name: libai 88 | # 对象的配置 89 | teacher: 90 | name: libai 91 | age: 18 92 | address: fujian 93 | # 行内对象配置 94 | person: {name: zhangsan, age: 18} 95 | # 配置数组 96 | city: 97 | - beijing 98 | - shanghai 99 | - nanjing 100 | - shenzhen 101 | citys: [beijing, shanghai, fujian] 102 | # 配置集合(对象数据) 103 | students: 104 | - name: li 105 | age: 15 106 | address: shanghai 107 | - name: mi 108 | age: 13 109 | address: guangzhou 110 | studentss: [{name: li, age: 15, address: shanghai}, {name: mi, age: 13, address: guangzhou}] 111 | # Map配置 112 | server: 9999 113 | 114 | 115 | ``` 116 | 117 | * 配置文件的读取 118 | 1. 通过@Value注入的方式.要访问yml的某值, 可以先定义个属性,然后通过```@Value("${xx}")```的方式来获取对应值. 119 | 120 | ```java 121 | @Controller 122 | public class YmlTestController { 123 | @Value("${name}") 124 | // 定义一个name属性,通过@Value注入, 然后就会去application.yml中找name这个属性的值来映射赋值 125 | private String name; 126 | @Value("${teacher.address}") 127 | private String address; 128 | @RequestMapping("/getYml") 129 | @ResponseBody 130 | public String ymlTest() { 131 | // 获取配置文件的信息 132 | return "address:" + address; 133 | } 134 | } 135 | 136 | ``` 137 | 138 | 2. 使用```@ConfigurationPropertiess(prefix="xx")```注解方式 139 | 通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射, 这种方式在yml中对应属性配置时会有提示. 140 | 1)在pom.xml导入依赖jar包. 141 | 142 | ```java 143 | 144 | org.springframework.boot 145 | spring-boot-configuration-processor 146 | true 147 | 148 | ``` 149 | 2) 在控制器中,配置 ```@ConfigurationPropertiess(prefix="xx")```注解,xx为配置文件的对象名(key前缀);定义需要获取配置文件中的属性,设置对应属性的set和get方法.映射完成. 具体实现参照: 150 | 151 | ```java 152 | @Controller 153 | // 添加注解, 映射yml中的person对象 154 | @ConfigurationProperties(prefix = "person") 155 | public class YmlTestController { 156 | // 定义需要获取的属性 157 | private String name; 158 | private String age; 159 | 160 | @RequestMapping("/getYml") 161 | @ResponseBody 162 | public String ymlTest() { 163 | // 获取配置文件的信息 164 | return "name:" + name +", age:" + age; 165 | } 166 | // 设置set和get方法 167 | public String getName() { 168 | return name; 169 | } 170 | 171 | public void setName(String name) { 172 | this.name = name; 173 | } 174 | 175 | public String getAge() { 176 | return age; 177 | } 178 | 179 | public void setAge(String age) { 180 | this.age = age; 181 | } 182 | 183 | } 184 | 185 | ``` 186 | 187 | ##### springBoot与其他框架的整合 188 | 1. 基于spring initalizr新建一个携带spring web的工程, 导入mybatis的起步依赖jar包和mysql的jar包. 189 | 190 | ```java 191 | 192 | org.mybatis.spring.boot 193 | mybatis-spring-boot-starter 194 | 1.3.2 195 | 196 | 197 | mysql 198 | mysql-connector-java 199 | 200 | 201 | ``` 202 | 2. 在application.properties中添加数据量的连接信息. 连接的信息的key值需要在jar包下的springboot-autoconfigure下的MATA-INF下的metadata.json文件中查找对应key的name然后进行设置, 如果key不对的话最终数据库是无法映射成功的. 203 | 然后在application.properties中配置数据库连接信息. 如: 204 | 205 | ```java 206 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 207 | spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8 208 | spring.datasource.username=root 209 | spring.datasource.password=12345678 210 | 211 | ``` 212 | 3. 提供一个数据库, 对应一个实体对象, 同时resources下提供一个与实体类对应的mapper文件. 213 | 214 | ```java 215 | 216 | //--------- AccountMapper-------- 217 | // @Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中 218 | @Mapper 219 | public interface AccountMapper { 220 | public List findAllAccount(); 221 | } 222 | //--------- AccountMapper.xml-------- 223 | 224 | 225 | 226 | 227 | 230 | 231 | //---------测试控制器-------- 232 | @Controller 233 | public class AccountController { 234 | @Autowired 235 | private AccountMapper accountMapper; 236 | @RequestMapping("/getAccountList") 237 | @ResponseBody 238 | public List getAccountList() { 239 | List list = accountMapper.findAllAccount(); 240 | return list; 241 | } 242 | } 243 | 244 | ``` 245 | 246 | 247 | 248 | 4. 在application.properties中添加mybatis的信息.配置字段的key值也是需要在jar包下的springboot-autoconfigure下的MATA-INF下的metadata.json文件中查找对应key的name然后进行设置.如果key值不对会导致无法映射成功. 249 | 250 | ```java 251 | #spring集成Mybatis环境 252 | #pojo别名扫描包 253 | mybatis.type-aliases-package=com.mine.domain 254 | #加载Mybatis映射文件 255 | mybatis.mapper-locations=classpath:mapper/*Mapper.xml 256 | 257 | 258 | 259 | ``` 260 | 261 | ##### 集成junit 262 | 263 | 1. 导入起步依赖坐标 264 | ```java 265 | 266 | org.springframework.boot spring-boot-starter-test test 267 | 268 | 269 | ``` 270 | 2. 直接利用注解的方式指定. 271 | ```@SpringBootTest```的属性指定的是引导类的字节码对象 272 | ```java 273 | @RunWith(SpringRunner.class) 274 | @SpringBootTest(classes = MybatisApplication.class) 275 | public class MybatisTest { 276 | @Autowired 277 | private AccountMapper accountMapper; 278 | @Test 279 | public void getlist() { 280 | List list = accountMapper.findAllAccount(); 281 | System.out.println(list); 282 | } 283 | } 284 | 285 | ``` -------------------------------------------------------------------------------- /note/spring基于xml和注解的AOP实现.md: -------------------------------------------------------------------------------- 1 | 所谓AOP, 即指的事面向切面编程.AOP的优势主要在于在程序运行期间,可以实现在不修改源码的情况下对已有方法进行增强.AOP的本质其实使用的动态代理技术. 2 | 3 | ##### AOP的XML配置 4 | * 在resources下新建一个bean.xml的文件, 然后导入aop配置. 5 | 6 | ```java 7 | 8 | 15 | 16 | ``` 17 | * spring中基于xml的Aop配置步骤 18 | 1) 把通知的bean配置进来, 交给spring管理. 19 | 2) 使用aop:config标签来表示aop开始的位置. 20 | 3) 使用aop:aspect标签表示开始配置切面.其中 id属性表示给切面提供一个唯一标识; ref表示指定通知类bean的id. 21 | 4) 在aop:aspect标签内部使用对应的标签来配置通知类型. 22 | ```aop:before ```表前置通知, 切入点方法执行之前执行. 23 | ```aop:after-returning```配置后置通知,切入点方法正常执行之后。它和异常通知只能有一个执行; 24 | ```aop:after-throwing ```配置异常通知, 切入点方法执行产生异常后执行。它和后置通知只能执行一个; 25 | ```aop:after```用于配置最终通知,无论切入点方法执行时是否有异常, 它都会在其后面执行. 26 | ```aop:around```环绕通知. 27 | ```切入点表达式的写法:```execution(表达式). 28 | ```表达式格式:```访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表). 29 | 例如:public void com.mine.service.impl.AccountServiceImpl.saveAccount() 30 | ```关于表达式的写法``` 31 | a. 返回值可以使用*号,表示任意返回值 32 | b. 包名可以使用 * 号,表示任意包,但是有几级包,需要写几个* 33 | c. 使用..来表示当前包,及其子包. 34 | d. 类名可以使用*号,表示任意类. 35 | e. 方法名可以使用*号,表示任意方法. 36 | f. 参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数. 37 | ```java 38 | 39 | * com..*.*(*) 40 | ``` 41 | 42 | g. 参数列表可以使用..表示有无参数均可,有参数可以是任意类型. 43 | 44 | ```java 45 | * com..*.*(..) 46 | ``` 47 | 48 | h.全通配方式: 49 | ```java 50 | * *..*.*(..) 51 | ``` 52 | i. ```通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类.推荐写法 : ``` 53 | 54 | ```java 55 | execution(* com.mine.service.impl.*.*(..)) //推荐写法 56 | ``` 57 | 5) 关于aop配置标签 58 | method 用于指定通知中的方法名. 59 | pointcut 属性用于指定切入点表达式,该表达式的含义是指对当前的业务层中哪些方法增强. 60 | expression 用于指定表达式内容; 61 | pointcut-ref 指定切入点表达式的引用; 62 | ```java 63 | 64 | ``` 65 | 66 | aop配置实例: 67 | 68 | ```java 69 | 70 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | ``` 82 | 83 | ##### 环绕通知 84 | ```aop:around```. 通常情况下,环绕通知都是独立使用的.spring 提供了一个接口''ProceedingJoinPoint'',它可以作为环绕通知的方法参数. 在环绕通知执行时,spring提供了一个"proceed()"方法可直接明确调用切入点方法,我们直接使用即可. 85 | 它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式. 86 | 在环绕通知中, 前后通知跟"proceed( )"方法的前后顺序相关, 在"proceed()"之前就表示是前置通知,之后则表示后置通知,在exception中表示异常通知, 在finally中则表示是最终通知. 87 | 实例: 88 | 89 | ```java 90 | public Object aroundPringLog(ProceedingJoinPoint pjp) { 91 | Object resValue = null; 92 | try { 93 | Object[] args = pjp.getArgs(); // 获取方法执行所需要的参数 94 | System.out.println("aroundPringLog中的logger方法开始记录日志了...前置"); 95 | resValue = pjp.proceed(args); // 明确调用业务层的切入点方法 ....切入点方法 96 | System.out.println("aroundPringLog中的logger方法开始记录日志了...后置"); 97 | return resValue; 98 | } catch (Throwable throwable) { 99 | System.out.println("aroundPringLog中的logger方法开始记录日志了...异常"); 100 | throw new RuntimeException(throwable); 101 | } finally { 102 | System.out.println("aroundPringLog中的logger方法开始记录日志了...最终"); 103 | 104 | } 105 | } 106 | 107 | ``` 108 | ##### AOP的注解配置 109 | 全注解形式配置, 不需要xml文件的方式,实例如下: 110 | ```java 111 | package com.mine.utils; 112 | 113 | import org.aspectj.lang.ProceedingJoinPoint; 114 | import org.aspectj.lang.annotation.*; 115 | import org.springframework.context.annotation.ComponentScan; 116 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 117 | import org.springframework.stereotype.Component; 118 | 119 | /** 120 | * 用于记录日志的工具类 121 | */ 122 | @Component("logger") 123 | @Aspect // 表示当前类是个切面类 124 | @EnableAspectJAutoProxy 125 | @ComponentScan(basePackages = "com.mine") 126 | public class Logger { 127 | 128 | @Pointcut("execution(* com..mine.service.impl.*.*(..))") 129 | private void pt(){} 130 | /** 131 | * 前置通知 132 | */ 133 | // @Before("pt()") 134 | public void beforePrintLog() { 135 | System.out.println("前置通知logger中的printLog方法开始执行了..."); 136 | } 137 | 138 | /** 139 | * 后置通知 140 | */ 141 | // @AfterReturning("pt()") 142 | public void afterReturningPrintLog() { 143 | System.out.println("后置通知logger中的printLog方法开始执行了..."); 144 | } 145 | 146 | /** 147 | * 异常通知 148 | */ 149 | // @AfterThrowing("pt()") 150 | public void afterThrowingPrintLog() { 151 | System.out.println("异常通知logger中的printLog方法开始执行了..."); 152 | } 153 | 154 | /** 155 | * 最终通知 156 | */ 157 | // @After("pt()") 158 | public void afterPrintLog() { 159 | System.out.println("最终通知logger中的printLog方法开始执行了..."); 160 | } 161 | @Around("pt()") 162 | public Object aroundPringLog(ProceedingJoinPoint pjp) { 163 | Object resValue = null; 164 | try { 165 | Object[] args = pjp.getArgs(); // 获取方法执行所需要的参数 166 | System.out.println("aroundPringLog中的logger方法开始记录日志了...前置"); 167 | resValue = pjp.proceed(args); // 明确调用业务层的切入点方法 ....切入点方法 168 | System.out.println("aroundPringLog中的logger方法开始记录日志了...后置"); 169 | return resValue; 170 | } catch (Throwable throwable) { 171 | System.out.println("aroundPringLog中的logger方法开始记录日志了...异常"); 172 | throw new RuntimeException(throwable); 173 | } finally { 174 | System.out.println("aroundPringLog中的logger方法开始记录日志了...最终"); 175 | 176 | } 177 | } 178 | } 179 | 180 | 181 | ``` 182 | 183 | 经过测试发现, spring基于注解的aop配置的四种通知存在顺序调用问题,若使用环绕通知则不存在这个问题.所以当我们选用注解的方式来进行aop配置时,推荐使用环绕通知,而非那四种通知模式. 184 | 修改测试类中的容器解析方式: 185 | 186 | ```java 187 | public class AopTest { 188 | public static void main(String[] args) { 189 | ApplicationContext cls = new AnnotationConfigApplicationContext(Logger.class); 190 | IAccountService accountService = cls.getBean("accountService", IAccountService.class); 191 | accountService.saveAccount(); 192 | } 193 | } 194 | 195 | ``` 196 | -------------------------------------------------------------------------------- /note/spring系统中常用的注解梳理.md: -------------------------------------------------------------------------------- 1 | ```@Component```作用:通用注解,通常用于标注某个不太容易判断应该归属于哪个分层的spring组件;即把资源让 spring 来管理,相当于在 xml 中配置一个 bean。 2 | ```@Repository```用于标注数据访问组件,即DAO组件。 3 | ```@Service```一般用于标注业务层 的注解。 4 | ```@Controller```一般用于表现层的注解, 比如struts中的action等。 5 | 上述注解均有属性值value:指定 bean 的 id,如果不指定 value 属性,默认 bean 的 id 是当前类的类名,首字母小写。 6 | ```@Autowired``` 7 | 作用:自动按照类型注入。当有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到则会报错。 8 | ```@Qualifier``` 9 | 作用:在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和@Autowire 一起使用;但是给方法参数注入时,可以独立使用。 10 | 属性:value:可以用来指定 bean 的 id。 11 | ```@Resource``` 12 | 作用:直接按照 Bean 的 id 注入。 13 | 属性:name:指定 bean 的 id。 14 | ```@Value``` 15 | 作用:注入基本数据类型和 String 类型数据的。 16 | 属性:value:用于指定值。 17 | ```@Scope``` 18 | 作用:指定 bean 的作用范围。 19 | 属性:value:指定范围的值。 20 | 属性value的取值通常包括:singleton/prototype/request/session/globalsession。 21 | 四种常见的 Spring Bean 的作用域: 22 | ①singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的。 23 | ②prototype : 每次请求都会创建一个新的 bean 实例。 24 | ③request : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。 25 | ④session : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。 26 | ```@PostConstruct``` 27 | 作用:用于指定初始化方法。@PostConstruct和@PreDestroy大致相当于元素的 init-method 属性和 destroy-method 属性指定的方法。@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。该注解的方法在整个Bean初始化中的执行顺序为:Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注解的方法)。 28 | ```@PreDestroy``` 29 | 作用:用于指定销毁方法。 30 | ```@Configuration``` 31 | 作用:用于指定当前类是一个 spring 配置类,也可以使用 @Component来代替,不过使用Configuration注解声明一个配置类显得语义更加清晰,更加严谨。 32 | ```@ComponentScan``` 33 | 作用:用于指定 spring 在初始化容器时要扫描的包。和在 spring 的 xml 配置文件中配置的作用是一样的。: 34 | ```java 35 | 36 | ``` 37 | 属性:basePackages:用于指定要扫描的包。和注解中的 value 属性作用一样。 38 | 39 | ```java 40 | @Configuration 41 | @ComponentScan("com.mine.test") 42 | public class SpringConfiguration { 43 | } 44 | ``` 45 | ```@Bean``` 46 | 作用: 47 | 该注解**只能写在方法**上,表明使用此方法创建一个对象,并且放入 spring 容器。 48 | 属性:name:给当前@Bean 注解方法创建的对象指定一个名称(即 bean 的 id)。 49 | ```@PropertySource``` 50 | 用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。 51 | 属性: 52 | value[]:用于指定 properties 文件位置。如果是在类路径下,需要写上classpath: 53 | 54 | ```java 55 | @Configuration 56 | @PropertySource("classpath:jdbc.properties") 57 | public class JdbcConfig{ 58 | } 59 | ``` 60 | 61 | ```@Import``` 62 | 作用:用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。当然,写上也没问题。 63 | 属性:value[]:用于指定其他配置类的字节码。 64 | ```java 65 | @Configuration 66 | @ComponentScan(basePackages = "com.mine.spring") 67 | @Import({ JdbcConfig.class}) 68 | public class SpringConfiguration { 69 | } 70 | ``` 71 | 72 | ```@ContextConfiguration ``` 73 | locations 属性:用于指定配置文件的位置。如果是类路径下,需要用 classpath:表明。 74 | classes 属性:用于指定注解的类。当不使用 xml 配置时,需要用此属性指定注解类的位置。 75 | 76 | ```java 77 | @RunWith(SpringJUnit4ClassRunner.class) 78 | @ContextConfiguration(locations= {"classpath:bean.xml"}) 79 | public class AccountServiceTest { 80 | } 81 | ``` 82 | ```@SpringBootApplication``` 83 | 点击项目中的@SpringBootApplication注解查看源码。如下: 84 | ```java 85 | @Target(ElementType.TYPE) 86 | @Retention(RetentionPolicy.RUNTIME) 87 | @Documented 88 | @Inherited 89 | @SpringBootConfiguration 90 | @EnableAutoConfiguration 91 | @ComponentScan(excludeFilters = { 92 | @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), 93 | @Filter(type = FilterType.CUSTOM, classes = 94 | AutoConfigurationExcludeFilter.class) }) 95 | public @interface SpringBootApplication { 96 | /** 97 | * Exclude specific auto-configuration classes such that they will never be 98 | applied. 99 | * @return the classes to exclude 100 | */ 101 | @AliasFor(annotation = EnableAutoConfiguration.class) 102 | Class[] exclude() default {}; 103 | ... ... ... 104 | } 105 | ``` 106 | 其中, 107 | ```@SpringBootConfiguration```相当于```@Configuration```,即标注该类是Spring的一个配置类。 108 | ```@EnableAutoConfiguration```表示开启SpringBoot的自动配置功能。因此,```@SpringBootApplication我们可以看作是@SpringBootConfiguration、@EnableAutoConfiguration与@ComponentScan这三个注解的集合体。``` 109 | 通过查看@EnableAutoConfiguration的源码,发现存在一个@AutoConfigurationPackage注解的类,而其中通过@Import(AutoConfigurationImportSelector.class) 导入了AutoConfigurationImportSelector类,而AutoConfigurationImportSelector中,由此可以知道导入哪些组件的选择器;将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中。具体的就是AutoConfigurationImportSelector中存在```SpringFactoriesLoader.loadFactoryNames 方法的作用就是从类路径下的META-INF/spring.factories文件中读取获取EnableAutoConfiguration指定的值```。在spring.factories 中存在大量的通过EnableAutoConfiguration指定的以Configuration为结尾的类名称,这些类就是存有自动配置信息的类,而SpringApplication在获取这些类名后会依次加载。 110 | ```java 111 | @Target(ElementType.TYPE) 112 | @Retention(RetentionPolicy.RUNTIME) 113 | @Documented 114 | @Inherited 115 | @AutoConfigurationPackage 116 | @Import(AutoConfigurationImportSelector.class) 117 | public @interface EnableAutoConfiguration { 118 | ... ... ... 119 | } 120 | ``` 121 | ```@GetMapping("names")```等价于```@RequestMapping(value="/names",method=RequestMethod.GET)``` 122 | ```@PostMapping("names")```等价于```@RequestMapping(value="/names",method=RequestMethod.POST)``` 123 | ```@RestController``` 124 | ```@RestController```的作用相当于```@Controller和@ResponseBody的合体```,表示这是个控制器 bean,并且是将函数的返回值直接填入 HTTP 响应体中。```仅使用 @Controller注解 而不加 @ResponseBody```,多用于返回一个页面视图的场景,这种情况常见于前后端不分离的比较传统的 Spring MVC 应用。(当我们仅仅使用 @Controller注解,那么Spring 默认会根据返回的页面视图名称去 resources 目录下 templates 目录下找对应的视图并返回,所以通常也建议把页面放在 resources/templates 目录下)。而```@Controller +@ResponseBody```则表示返回 JSON 或 XML 形式数据。 125 | 126 | ```java 127 | @RestController 128 | public class MineController { 129 | @PostMapping("/login") 130 | public User HelloUser(@RequestBody User user) { 131 | return User; 132 | } 133 | 134 | } 135 | ``` 136 | ```@RequestBody``` 137 | 主要用来```接收前端传递给后端的json字符串```中的数据的(请求体中的数据的);GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。在同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。 138 | ```@RequestParam```用于获取查询参数。 139 | ```@PathVariable```用于映射 URL 绑定的占位符,也就是获取路径参数。通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中。例如:@RequestMapping("/testPathVariable/{id}") 140 | 141 | ```java 142 | @RequestMapping("/testPathVariable/{id}") 143 | public String testPathVariable(@PathVariable("id") Integer id) { 144 | System.out.println("testPathVariable:" + id); 145 | return SUCCESS; 146 | } 147 | ``` 148 | ```@Value``` 149 | 我们可以通过@Value注解将配置文件中的值映射到一个Spring管理的Bean的字段上。只能用于获取业务逻辑中的某个配置文件中的某项值,不支持获取复杂的数据类型,如map等。 150 | ```java 151 | //在application.properties或application.yml中配置信息如下: 152 | lable:我来测试一下abd 153 | person: 154 | name: libai 155 | age: 45 156 | // 实体bean代码: 157 | @Controller 158 | public class QuickStartController { 159 | @Value("${person.name}") 160 | private String name; 161 | @Value("${person.age}") 162 | private Integer age; 163 | @RequestMapping("/mineTest") 164 | @ResponseBody 165 | public String mineTest(){ 166 | return "springboot 测试成功! name="+name+",age="+age; 167 | } 168 | } 169 | ``` 170 | ```@ConfigurationProperties``` 171 | 作用:通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射。但需要字段必须提供set方法才可以,而使用@Value注解修饰的字段不需要提供set方法。(获取全局配置文件的值),比@value更强大,支持复杂类型数据的获取。 172 | 173 | ```java 174 | person: 175 | name: libai 176 | age: 45 177 | //----------------- 178 | @Controller 179 | @ConfigurationProperties(prefix = "person") 180 | public class QuickStartController { 181 | private String name; 182 | private Integer age; 183 | @RequestMapping("/mineTest") 184 | @ResponseBody 185 | public String mineTest(){ 186 | return "springboot 测试成功! name="+name+",age="+age; 187 | } 188 | public void setName(String name) { 189 | this.name = name; 190 | } 191 | public void setAge(Integer age) { 192 | this.age = age; 193 | } 194 | } 195 | ``` 196 | ```@PropertySource```加载指定的 properties 文件。读取指定的文件后,可以通过@Value获取配置字段的值。 197 | 198 | ```java 199 | @PropertySource(value = "classpath:application.properties",ignoreResourceNotFound = false) 200 | ``` 201 | ```@ImportResource```:作用是导入spring的配置文件, 让配置文件里面的内容生效.spring boot里面若没有spring的配置文件, 我们自己编写配置之后,也无法自动识别.想让编写的配置文件生效,加载进来.可以通过@ImportResource标注在一个配置类上来实现. 202 | 203 | ```java 204 | @Configuration 205 | @ImportResource("classpath:bean.xml") //导入xml配置项 206 | public class MineSystemConfig { 207 | 208 | } 209 | ``` 210 | --------------------------------------------------------------------------------