Maven基础&&Spring框架阶段常用工具类整理
常用坐标:maven常用坐标,spring常用坐标
常用工具类
1.密码加密工具类:
package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE64Encoder; public class MD5Util { /** * 密码加密 * @param password * @return * @throws Exception */ public static String md5(String password){ try { //1.创建加密对象 MessageDigest md5 = MessageDigest.getInstance("md5"); //2.加密密码 byte[] by = md5.digest(password.getBytes()); //3.创建编码对象 BASE64Encoder encoder = new BASE64Encoder(); //4.对结果编码 return encoder.encode(by); }catch (Exception e){ throw new RuntimeException(e); } } }
2.事务控制工具类:
package com.itheima.utils; import org.apache.ibatis.session.SqlSession; public class TransactionUtil { /** * 提交释放 * @param sqlSession */ public static void commit(SqlSession sqlSession){ if(sqlSession!=null) { sqlSession.commit(); } } /** * 回滚释放 * @param sqlSession */ public static void rollback(SqlSession sqlSession){ if(sqlSession!=null) { sqlSession.rollback(); } } /** * 单独释放 * @param sqlSession */ public static void close(SqlSession sqlSession){ if(sqlSession!=null) sqlSession.close(); } }
3.填充表单数据到javabean的工具类:
package com.itheima.utils; import javax.servlet.http.HttpServletRequest; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.converters.DateConverter; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.servlet.ServletFileUpload; import java.beans.PropertyDescriptor; import java.io.File; import java.lang.reflect.Method; import java.util.Date; import java.util.List; /** * 填充表单数据到javabean的工具类 * */ public class BeanUtil { /** * 封装表单中的数据到javabean中 * @param request 表单中的数据 * @param clazz 封装到哪个javabean * @return 封装好的javabean对象 * 使用的是泛型。泛型必须先声明再使用。声明必须在返回值之前 * T指的就是泛型,它可以是任意字符,只是作为一个占位符。 * 声明时用什么字符,使用时就得用什么 */ public static <T> T fillBean(HttpServletRequest request,Class<T> clazz){ //1.定义一个T类型的javabean T bean = null; try{ //2.实例化bean对象 bean = clazz.newInstance(); //3.使用BeanUtils的方法进行封装 BeanUtils.populate(bean, request.getParameterMap()); //4.返回 return bean; }catch(Exception e){ throw new RuntimeException(e); } } /** * 封装表单中的数据到javabean中,带有日期格式数据 * @param request 表单中的数据 * @param clazz 封装到哪个javabean * @return 封装好的javabean对象 * 使用的是泛型。泛型必须先声明再使用。声明必须在返回值之前 * T指的就是泛型,它可以是任意字符,只是作为一个占位符。 * 声明时用什么字符,使用时就得用什么 */ public static <T> T fillBean(HttpServletRequest request,Class<T> clazz,String datePattern){ //1.定义一个T类型的javabean T bean = null; try{ //2.实例化bean对象 bean = clazz.newInstance(); //3.创建日期转换器对象 DateConverter converter = new DateConverter(); converter.setPattern(datePattern); //4.设置转换器 ConvertUtils.register(converter, Date.class); //5.使用BeanUtils的方法进行封装 BeanUtils.populate(bean, request.getParameterMap()); //6.返回 return bean; }catch(Exception e){ throw new RuntimeException(e); } } /** * 文件上传的表单填充 * @param items 文件上传的表单项 * @param clazz 要封装的实体类字节码 * @param <T> 泛型 * @return 返回封装好的对象 */ public static <T> T fillBean(List<FileItem> items,Class<T> clazz){ //1.定义一个T类型的javabean T bean = null; try{ //2.实例化Bean bean = clazz.newInstance(); //3.遍历文件项集合 for(FileItem item : items){ //4.判断是不是文件 if(item.isFormField()){//表单字段,不是文件 //5.取出表单中的name属性取值 String fieldName = item.getFieldName(); //6.使用UTF-8字符集取出表单数据 String fieldValue = item.getString("UTF-8"); //7.创建属性描述器对象 PropertyDescriptor pd = new PropertyDescriptor(fieldName,clazz); //8.获取写方法(setXXX方法) Method method = pd.getWriteMethod(); //9.把数据填充到bean中 method.invoke(bean,fieldValue); } } //10.返回 return bean; }catch(Exception e){ throw new RuntimeException(e); } } }
4.dao接口代理实现类的工厂:
package com.itheima.factory; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; /** * 用于生成dao接口代理实现类的工厂 */ public class MapperFactory { private static SqlSessionFactory factory; private static ThreadLocal<SqlSession> tl = new ThreadLocal<SqlSession>(); static { InputStream in = null; try { //1.读取mybatis主配置文件 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.创建构建者对象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3.使用构建者创建SqlSessionFactory工厂 factory = builder.build(in); }catch (Exception e){ //打印异常信息到控制台 e.printStackTrace(); //抛出错误提示程序终止执行 throw new ExceptionInInitializerError("初始化SqlSessionFactory失败"); }finally { //释放流对象 if(in != null){ try{ in.close(); }catch (Exception e){ e.printStackTrace(); } } } } /** * 获取SqlSession对象 * @return * 保留此方法是为了后面对业务层方法增强,利用AOP添加事务 */ public static SqlSession getSqlSession(){ return factory.openSession(false); } /** * 获取Dao接口的代理实现类 * @param daoClass dao接口字节码 * @return */ public static <T> T getMapper(SqlSession sqlSession,Class<T> daoClass){ //1.判断传入的SqlSession是否为null if(sqlSession == null){ return null; } //2.不为null,创建代理实现类 return sqlSession.getMapper(daoClass); } }
5.解决请求响应乱码:
package com.itheima.web.controller.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebFilter(value = "/*",initParams={@WebInitParam(name = "encoding",value = "UTF-8")}) public class CharacterEncodingFilter implements Filter { private FilterConfig filterConfig; /** * 初始化方法,获取过滤器的配置对象 * @param filterConfig * @throws ServletException */ @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { //1.定义和协议相关的请求和响应对象 HttpServletRequest request ; HttpServletResponse response; try{ //2.把参数转换成协议相关的对象 request = (HttpServletRequest)req; response = (HttpServletResponse)resp; //3.获取配置的字符集 String encoding = filterConfig.getInitParameter("encoding"); //4.设置请求参数的字符集 request.setCharacterEncoding(encoding); //5.设置响应对象输出响应正文时的字符集 response.setContentType("text/html;charset=UTF-8"); //6.放行 chain.doFilter(request,response); }catch (Exception e){ e.printStackTrace(); } } @Override public void destroy() { //可以做一些清理操作 } }
6.批量注入工具类:
package config.registrar; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.filter.AspectJTypeFilter; import org.springframework.core.type.filter.TypeFilter; import java.io.IOException; import java.util.Map; import java.util.Properties; public class CustomeImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { private String expression; public CustomeImportBeanDefinitionRegistrar(){ try { //初始化时指定加载的properties文件名 Properties loadAllProperties = PropertiesLoaderUtils.loadAllProperties("import.properties"); //设定加载的属性名 expression = loadAllProperties.getProperty("path"); } catch (IOException e) { e.printStackTrace(); } } @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { //1.定义扫描包的名称 String[] basePackages = null; //2.判断有@Import注解的类上是否有@ComponentScan注解 if (importingClassMetadata.hasAnnotation(ComponentScan.class.getName())) { //3.取出@ComponentScan注解的属性 Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName()); //4.取出属性名称为basePackages属性的值 basePackages = (String[]) annotationAttributes.get("basePackages"); } //5.判断是否有此属性(如果没有ComponentScan注解则属性值为null,如果有ComponentScan注解,则basePackages默认为空数组) if (basePackages == null || basePackages.length == 0) { String basePackage = null; try { //6.取出包含@Import注解类的包名 basePackage = Class.forName(importingClassMetadata.getClassName()).getPackage().getName(); } catch (ClassNotFoundException e) { e.printStackTrace(); } //7.存入数组中 basePackages = new String[] {basePackage}; } //8.创建类路径扫描器 ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false); //9.创建类型过滤器(此处使用切入点表达式类型过滤器) TypeFilter typeFilter = new AspectJTypeFilter(expression,this.getClass().getClassLoader()); //10.给扫描器加入类型过滤器 scanner.addIncludeFilter(typeFilter); //11.扫描指定包 scanner.scan(basePackages); } }
常用坐标
spring常用坐标:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!--spring环境--> <!--spring环境--> <!--spring环境--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--mybatis环境--> <!--mybatis环境--> <!--mybatis环境--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!--mysql环境--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--spring整合jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--spring整合mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <!--druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <!--AOP--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!--分页插件坐标--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <!--springmvc环境--> <!--springmvc环境--> <!--springmvc环境--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--jackson相关坐标3个--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> <!--servlet环境--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--jsp坐标--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!--导入校验的jsr303规范--> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <!--导入校验框架实现技术--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.0.Final</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.3.2.Final</version> </dependency> <dependency> <groupId>com.fasterxml</groupId> <artifactId>classmate</artifactId> <version>1.3.4</version> </dependency> <!--其他组件--> <!--其他组件--> <!--其他组件--> <!--junit单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--spring整合junit--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <!--Dubbo的起步依赖,版本2.7之后统一为org.apache.dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.4.1</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.39.Final</version> </dependency> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.23</version> </dependency> <!--ZooKeeper客户端实现 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> </dependency> <!--ZooKeeper客户端实现 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.0</version> </dependency> <!--web开发的起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.3</version> </dependency> <!--引入es的坐标--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.4.0</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.4.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.8.RELEASE</version> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> </build>
meaven基础常用坐标
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!--mybatis_--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- servlet3.0 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--jsp--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <!--bean-utils--> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency> <!--apache工具包--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <!--jstl--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!--POI--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.0.1</version> </dependency> </dependencies> <build> <plugins> <!--tomcat插件--> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>/</path> <uriEncoding>utf-8</uriEncoding> </configuration> </plugin> </plugins> </build>