admin 管理员组文章数量: 887006
使用Idea2019创建ssm项目,SpringMVC+Spring+MyBatis+Maven整合
源码下载地址:
链接:
提取码:06qn
1、创建项目
2、完善Java目录结构,如果生成有,就可以跳过这一步
2.1、创建java目录结构
2.2、改变目录结构
3、添加框架所需要的依赖(只拿有用的配置,不要整个都拿过去)
<?xml version="1.0" encoding="UTF-8"?><project xmlns=".0.0" xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>ssm</groupId><artifactId>ssm</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>ssm Maven Webapp</name><!-- FIXME change it to the project's website --><url>;/url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><mavenpiler.source>1.7</mavenpiler.source><mavenpiler.target>1.7</mavenpiler.target><spring.version>4.2.4.RELEASE</spring.version><mybatis.version>3.2.8</mybatis.version><mybatis.spring.version>1.2.2</mybatis.spring.version><slf4j.version>1.6.4</slf4j.version><log4j.version>1.2.17</log4j.version><jackson.version>2.4.2</jackson.version><druid.version>1.0.9</druid.version><mysql.version>5.1.32</mysql.version><jstl.version>1.2</jstl.version><servlet-api.version>2.5</servlet-api.version><jsp-api.version>2.0</jsp-api.version><commons-io.version>1.3.2</commons-io.version><commons-fileupload.version>1.3.1</commons-fileupload.version></properties><!-- 版本锁定 --><dependencyManagement><dependencies><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis.spring.version}</version></dependency><!-- 日志处理 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><!-- Jackson Json处理工具包 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- io包 --><dependency><groupId>org.apachemons</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency><!-- 文件上传组件 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>${commons-fileupload.version}</version></dependency><!-- JSP相关 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>${servlet-api.version}</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>${jsp-api.version}</version><scope>provided</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId></dependency><!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId></dependency><!-- 日志处理 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency><!-- Jackson Json处理工具包 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- io包 --><dependency><groupId>org.apachemons</groupId><artifactId>commons-io</artifactId></dependency><!-- 文件上传组件 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId></dependency><!-- JSP相关 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><scope>provided</scope></dependency></dependencies><build><finalName>ssm</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- java编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see .html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement><resources><!--配置资源文件映射 --><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build>
</project>
4、在resources目录下添加配置文件(没有此目录请自行创建,且改变目录结构,和上面一样)
5、mybatis配置文件 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis//DTD Config 3.0//EN"".dtd"><configuration><!-- 给实体类起别名 --><typeAliases ><package name="xuan.entity"/></typeAliases></configuration>
6、Spring核心配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi=""xmlns=""xmlns:context=""xmlns:tx=""xsi:schemaLocation=" .2.xsd .2.xsd .2.xsd"><!-- 加载数据库连接信息配置文件 --><context:property-placeholder location="classpath:db.properties" /><!--druid连接池 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${druid.driver}"/><property name="url" value="${druid.url}"/><property name="username" value="${druid.username}"/><property name="password" value="${druid.password}"/></bean><!-- 配置Mybatis工厂 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml" /></bean><!-- Mapper动态代理开发 扫包 给定包下的接口文件名和映射文件名必须相同 创建接口的实现类--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="xuan.mapper" /></bean><!-- 开启事务 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 事务注解驱动--><tx:annotation-driven /><context:component-scan base-package="xuan.service"/>
</beans>
7、springmvc配置文件 springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xmlns:mvc=""xmlns:context=""xsi:schemaLocation="://www.springframework/schema/beans/spring-beans.xsd://www.springframework/schema/context/spring-context.xsd://www.springframework/schema/mvc/spring-mvc.xsd"><!-- SpringMVC使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter --><mvc:annotation-driven /><!-- 启动注解 --><!-- 配置扫描注解 @Controller @Service --><context:component-scan base-package="xuan.controller" /><!-- 配置静态资源映射 --><!--<mvc:resources location="/js/" mapping="/js/**"/><mvc:resources location="/css/" mapping="/css/**"/><mvc:resources location="/imgs/" mapping="/imgs/**"/><mvc:resources location="/font/" mapping="/font/**"/>--><!-- 配置视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置逻辑视图的前缀 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 配置逻辑视图的后缀 --><property name="suffix" value=".jsp" /></bean>
</beans>
9、数据库配置文件
druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/xuan?characterEncoding=utf-8
druid.username=root
druid.password=root
10、配置web.xml文件
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"".dtd" >
<web-app><display-name>Archetype Created Web Application</display-name><!-- 指定spring核心配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 处理POST提交乱码问题 --><filter><filter-name>encoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- spring监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置前端控制器 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 指定配置文件位置和名称 如果不设置,默认找/WEB-INF/<servlet-name>-servlet.xml --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
11、完善目录结构,创建实体类,dao层,service层,controller层,目录结构如下
12、配置tomcat运行
13、数据库数据如下
14、启动Tomcat,访问地址 http://localhost:1314/ssm_war/GetAll ,出现下面效果,表示成功
本文标签: 使用Idea2019创建ssm项目,SpringMVCSpringMyBatisMaven整合
版权声明:本文标题:使用Idea2019创建ssm项目,SpringMVC+Spring+MyBatis+Maven整合 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732352974h1533617.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论