SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

SSM Spring+SpringMVC+mybatis+maven环境搭建

1、首先右键点击项目区空白处,选择new-》other..在弹出框中输入maven,选择Maven Project。

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

默认next下一步

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

选中图片所选next下一步

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

输入其中所需参数点击Finish完成创建操作

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

2、标准的maven结构,但是还需要创建几个文件夹,右键点击src下的main-》new-》Folder填写java,右键点击src new-》Folder填写test,同理在test下创建java folder。

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

在src/main/java下创建基本的层级,如图:

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

上图中jsp的报错暂时不用管,具体原因尚不清楚,不过不影响项目开发启动,在pom.xml下添加框架必须的包:

 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.cjl.ssm</groupId>
<artifactId>SSM</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SSM</name> <!-- 编码 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> <!--Maven依赖 需要更换可以到http://mvnrepository.com进行查询-->
<dependencies> <!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency> <!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency> <!-- mybatis 出现分页使用异常,极有可能是版本问题-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- mybatis pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.3</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency> <!--servlet、基本包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency> <!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!--dbcp连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> <!--json、基本包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.23</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.5</version>
</dependency> <!-- 日志文件管理包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <!-- junit4 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> </dependencies> <!--项目编译及其构建-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>SSM</warName>
<!-- <packagingExcludes>
**/resources/**
</packagingExcludes> -->
<webResources>
<resource>
<directory>src/main/resources/resources/config</directory>
<targetPath>WEB-INF/classes/resources/config</targetPath>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<inherited>true</inherited>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<!-- 此处由3.2升级为3.3.原因是maven版本3.1的,需要3,3才能兼容.否则会报错:java.lang.NoClassDefFoundError:
org/sonatype/aether/graph/DependencyFilter -->
<version>3.3</version>
<configuration>
<locales>zh_CN</locales>
</configuration>
</plugin> </plugins>
</build> </project>

mavenjar包下载完成后需要在resources文件夹下添加几个配置文件

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

applicationContext-mybatis.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- Mybatis 和 Spring的整合 -->
<!-- 1.数据源:DriverManagerDataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- 2.Mybatis 的 SqlSession的工厂:SqlSessionFactoryBean dataSource引用数据源 Mybatis
定义数据源,同意加载配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean> <!-- 3. Mybatis自动扫描加载Sql映射文件/接口:MapperScannerConfigurer sqlSessionFactory
basePackage:指定sql映射文件/接口所在的包(自动扫描) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cjl.mapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> <!-- 4.事务管理:DataSourceTransactionManager dataSource 引用上面定义好的数据源 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 5.使用声明式事务: transaction-manager = "txManager" tx:advice 这种 是用 aop方式管理事物
annotation-driven 这种是注解方式管理事物 第一种方式,需要在spring配置文件配置一些参数 第二种方式,需要在 类里 加一些注解进行事物管理
用一种就行,没必须都用 -->
<tx:annotation-driven transaction-manager="txManager" /> </beans>

generatorConfig.xml 这个xml主要是为了使用mybatis生成代码用

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration> <!-- <properties resource="init.properties"/> --> <!-- 指定数据库连接驱动地址 -->
<classPathEntry
location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" />
<!-- 一个数据库一个 context -->
<context id="context1"> <commentGenerator>
<!-- 是否取消注释 -->
<property name="suppressAllComments" value="true"></property>
<!-- 是否生成注释代时间戳-->
<property name="suppressDate" value="true"></property>
</commentGenerator>
<!-- jdbc连接配置 -->
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/test" driverClass="com.mysql.jdbc.Driver" password="123456" userId="root"/> <!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- 生成实体类的地址 -->
<javaModelGenerator targetPackage="com.cjl.model"
targetProject="D:\eclipse-workspace_lt\ssm\src\main\java" /> <!-- 生成mapper.xml文件 -->
<sqlMapGenerator targetPackage="com.cjl.mapper"
targetProject="D:\eclipse-workspace_lt\ssm\src\main\resources" /> <!-- 生成mapxml对应的client 也就是接口dao -->
<javaClientGenerator targetPackage="com.cjl.mapper"
targetProject="D:\eclipse-workspace_lt\ssm\src\main\java" type="XMLMAPPER" /> <table schema="test" tableName="tb_department" domainObjectName="Department" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
<!-- domainObjectName 指定生成的类的名字 -->
</table>
</context>
</generatorConfiguration>

mybatis-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置打印 SQL 到控制台 以及懒加载 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
<!-- 打开延迟加载的全局开关 -->
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<!-- 为org.bb.ssm.mode.UserInfo 设置一个别名 UserInfo -->
<typeAliases>
<typeAlias type="com.cjl.model.User" alias="User"/>
</typeAliases>
<mappers>
<package name="com.cjl.mapper"/>
</mappers>
</configuration>

spring-MVC.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- 配置自定扫描包 -->
<context:component-scan base-package="com.cjl.controller"/>
<context:component-scan base-package="com.cjl.service.Impl"/> <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 处理静态资源 -->
<!-- <mvc:default-servlet-handler/> -->
<mvc:resources mapping="/vendor/**" location="/vendor/" />
<mvc:resources mapping="/dist/**" location="/dist/" />
</beans>

3、接下来创建一张表,已完成代码测试以及本次ssm框架搭建工作

 CREATE TABLE `user` (
`user_id` varchar(10) NOT NULL default '',
`user_name` varchar(50) default NULL,
`user_age` varchar(50) default NULL,
`department_id` int(10) default NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

创建好后需要在generatorConfig.xml生成代码配置文件中修改好适合自己项目的配置方法,右键点击项目选择Run as->Maven Build..->在goals中填写:mybatis-generator:generate -e 点击run

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

当看到构建成功后即可刷新项目

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

可以看到代码已经生成完成!!将项目加入tomcat启动

SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

可以看到项目部署已经完成!

上一篇:在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况


下一篇:postgresql 导出数据字典文档