一、maven管理
使用maven管理构建项目时,可能会出现各种问题,为了避免出现当前难以解决的问题,我们可以在使用maven时进行一系列的优化处理。
1、选择不使用maven中自带的模板,勾选Create a simple project (skip archetype selection)选项,点击下一步。
2、因为选择自己构建maven框架,所以会出现下面这个页面,比以前直接选择maven自带的模板需要填写的内容看起来要多一些,其实,只要按下图填好基本信息就好,直接点击finish。
3、按以上流程创建好项目环境后,会出现以下结构
这时候pom.xml报错是因为在webapp文件夹下缺少了WEN-INF文件夹以及其下的web.xml文件,自行创建好久OK。
4、添加好web.xml文件后基本配置已经完好,我们还可以设置其他一些配置,比如:
(1)window->perferences->选择maven,来到如下页面
设置installations,创建自己的maven文件,最好别选用第一个maven自带的选项,速度回很慢。
5、选择User Settings,将User Settings改为自己的maven下conf\settings.xml文件,当然该setting文件肯定是修改过的,关于如何改为阿里云镜像仓库,请自行问度年。
将maven中央仓库改为阿里云镜像仓库,基本实现秒下,但是在网速很好的前提下还是最好选择maven自带的中央仓库,因为阿里云镜像仓库与中央仓库可以更新没有同步,一些中央仓库的文件在阿里云里可能就还没有更新。
6、现在还需要完善框架结构,完善后如下图:
7、然后我们就可以下pom.xml文件中添加maven依赖文件了,详细如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qfedu</groupId>
<artifactId>hr</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<!-- Servlet / JSP / JSTL -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- ORM框架相关 -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- Spring / Spring MVC / Spring ORM / Spring Test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 工具类 -->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
这只是当前需要的依赖包,如果开发时不够用,可以再进行添加。
二、整合SSM+hibernate
*1、如果使用配置文件就需要如下操作:
在src/main/resources目录下创建spring配置文件:applicationContext.xml;
在WEN-INF目录下创建springMVC配置文件:dispatechar.xml。
2、使用纯代码配置文件,总体框架如下:
3、config包下的Java类就相当于applicationContext.xml文件,dispatechar-servlet.xml文件和web.xml文件。
MyWebInitializer.java
package com.qfedu.config;
import javax.servlet.Filter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// 指定Spring IoC容器的配置类
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
// 指定Spring MVC的配置类
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
// 指定如何映射前端控制器
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
// 配置过滤器
return new Filter[] {
new CharacterEncodingFilter("utf-8", true),
new HiddenHttpMethodFilter()
};
}
}
RootConfig.java
package com.qfedu.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.hibernate.SessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.alibaba.druid.pool.DruidDataSource;
@MapperScan("com.qfedu.mapper")
@Configuration
@EnableTransactionManagement
@ComponentScan("com.qfedu")
public class RootConfig {
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/hr?useUnicode=true&characterEncoding=utf8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
dataSource.setMinIdle(10);
dataSource.setMaxActive(100);
dataSource.setMaxWait(5000);
dataSource.setMinEvictableIdleTimeMillis(30000);
dataSource.setMaxEvictableIdleTimeMillis(120000);
dataSource.setPoolPreparedStatements(true);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
return dataSource;
}
@Bean
public LocalSessionFactoryBean localSessionFactoryBean(DataSource dataSource) {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("com.qfedu.domain");
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
hibernateProperties.setProperty("hibernate.show_sql", "true");
hibernateProperties.setProperty("hibernate.format_sql", "true");
factoryBean.setHibernateProperties(hibernateProperties);
return factoryBean;
}
@Bean
public SqlSessionFactory mybatisSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean.getObject();
}
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
}
配置了Druid连接池,hibernate的会话工厂和mybatis发的会话工厂,以及事务管理器。
WebConfig.java
package com.qfedu.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("com.qfedu.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setOrder(10);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
以上为整合框架的全部配置文件。