1.导包pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>cn.itsource.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>http://www.example.com</url> <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> <org.springframework.version>4.2.5.RELEASE</org.springframework.version> <org.hibernate.version>4.3.8.Final</org.hibernate.version> <spring-data-jpa.version>1.9.0.RELEASE</spring-data-jpa.version> <com.fasterxml.jackson.version>2.5.0</com.fasterxml.jackson.version> </properties> <dependencies> <!-- Spring的支持包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> <scope>test</scope> </dependency> <!-- 引入web前端的支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- SpringMCV上传需要用到io包--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <!-- 文件上传用到的包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <!-- SpringMVC的json支持包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${com.fasterxml.jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${com.fasterxml.jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${com.fasterxml.jackson.version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> <!-- 測試包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <!-- 这个scope 只能作用在编译和测试时,同时没有传递性。表示在运行的时候不添加此jar文件 --> <scope>provided</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!--mybatis--> <!--mybatis的核心包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> <!--mybatis集成spring的框架--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <!-- 日志包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <!--mybatis的代码生成器的核心包--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- jar包导入 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.1</version> </dependency> </dependencies> <build> <finalName>ssm</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>false</verbose> <overwrite>true</overwrite> </configuration> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <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> </build> </project>
2.搭建结构
3.配置相关配置文件
1.applicationContext.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--1.引入jdbc.properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--2.扫包:非Controller层--> <!--service层的扫描--> <context:component-scan base-package="cn.itsource.ssm.wy.service"/> <!-- 3.配置连接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!--连接数据4个属性 --> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!--maxActive: 最大连接数量 --> <property name="maxActive" value="150"/> <!--minIdle: 最小空闲连接 --> <property name="minIdle" value="5"/> <!--maxIdle: 最大空闲连接 --> <property name="maxIdle" value="20"/> <!--initialSize: 初始化连接 --> <property name="initialSize" value="30"/> <!-- 用来配置数据库断开后自动连接的 --> <!-- 连接被泄露时是否打印 --> <property name="logAbandoned" value="true"/> <!--removeAbandoned: 是否自动回收超时连接 --> <property name="removeAbandoned" value="true"/> <!--removeAbandonedTimeout: 超时时间(以秒数为单位) --> <property name="removeAbandonedTimeout" value="10"/> <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 --> <property name="maxWait" value="1000"/> <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. --> <property name="timeBetweenEvictionRunsMillis" value="10000"/> <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 --> <property name="numTestsPerEvictionRun" value="10"/> <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 --> <property name="minEvictableIdleTimeMillis" value="10000"/> <property name="validationQuery" value="SELECT NOW() FROM DUAL"/> </bean> <!--4.mybatis--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--dataSource--> <property name="dataSource" ref="dataSource" /> <!--private String typeAliasesPackage; 别名的配置--> <property name="typeAliasesPackage" value="cn.itsource.ssm.wy.domain"/> <!--private Resource[] mapperLocations;:mapper映射的配置--> <property name="mapperLocations" value="classpath*:cn/itsource/ssm/wy/mapper/*.xml" /> <!--lau UI分页插件--> <property name="plugins" > <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value>helperDialect=mysql</value> </property> </bean> </array> </property> </bean> <!--5.mapper接口的配置: 配置了这个mapper,以后再service就直接注入这个mapper就可以使用了 这种一个一个的配置,假如你有很多的mapper接口,就很麻烦了 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> --> <!--一劳永逸的mapper接口的配置: 直接扫这个mapper接口的包,把这个包下面的所有的类都注册成一个bean,以后在service直接注入就ok --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.itsource.ssm.wy.mapper" /> </bean> <!--6.事务的配置: 事务有两种方式:xml和注解方式(采纳service) 事务管理器: --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- transaction-manager的默认值就是找id="transactionManager"; 如果你的事务管理器的id是这个默认值,就可以不用写这个transaction-manager="transactionManager"; 否则就要显示指定事务管理器的id值 --> <tx:annotation-driven /> </beans>
2.applicationContext-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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 静态资源放行 扫包:Controller mvc注解 统一视图解析器 文件上传 --> <!-- 1.对静态资源进行放行 --> <mvc:default-servlet-handler /> <!-- 2.扫描controller部分的包 web controller--> <!-- @Component组件, @Repository持久层, @Service业务逻辑层, and @Controller控制器 --> <context:component-scan base-package="cn.itsource.ssm.wy.web.controller" /> <!-- 3.添加mvc对@RequestMapping等注解的支持 --> <mvc:annotation-driven/> <!-- 4.视图解析器 (struts2视图类型类似) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 设置视图路径的前后缀,该配置可以让我们写视图路径的时候更简单。 --> <!-- 希望跳转jsp是[/WEB-INF/views/前缀][xxx变量][.jsp后缀] --> <!-- * @see #setPrefix --> <property name="prefix" value="/WEB-INF/views/" /> <!-- * @see #setSuffix --> <property name="suffix" value=".jsp" /> </bean> <!--5.文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> </bean> </beans>
3.jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///smm jdbc.username=root jdbc.password=root
4.log4j.properties
#日志的根级别 # trace,debug,info,warn,error:级别越大,看到的日志信息越少 # 配置的级别,日志效果包含这个级别和以上的日志信息,向看到越多的日志,级别越小 log4j.rootLogger=ERROR, stdout #你自己的包路径:log4j.logger.加你自己的包路径 log4j.logger.cn.itsource.mybatis=TRACE log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
4.配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <!-- 1.告诉spring加载位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 2.配置监听器 目的启动spring配置--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--3.核心控制器: 默认加载在WEB-INF下找servlet-name-servlet.xml: dispatchServlet-servlet.xml--> <servlet> <servlet-name>dispatchServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatchServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
5.代码生成器
1.导包---上面已导(pom.xml红色)
2.配置插件(pom.xml绿色)
3.generatorConfig.xml
<?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> <!-- classPathEntry:可以配置多个,也不配置 数据库驱动:这里找到相应的驱动jar包就可以了(注:不同数据库的jar不一样) location:里面的是路径(也可以直接写绝对路径 -> 如:E:\mybatis\mysql-connector-java-5.1.26-bin.jar) --> <classPathEntry location="F:\soft\maven\repository-teacher\mysql\mysql-connector-java\5.1.26\mysql-connector-java-5.1.26.jar"/> <!-- context:用于生成一组对象的环境(至少配置1个,可以配置多个) id:表达唯一的名称 targetRuntime:用于指定生成的代码的运行环境(MyBatis3/MyBatis3Simple) MyBatis3:默认值 MyBatis3Simple:不会生成与Example(案例)相关的方法 --> <context id="DB2Tables" targetRuntime="MyBatis3Simple" > <!-- 用于配置如果生成注释信息(最多可以配置一下) suppressAllComments:阻止生成注释 ,默认为false suppressDate:阻止生成的注释 时间戳,默认为false addRemarkComments:注释是否添加数据库表的备注信息,默认为false --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 这个应该比较清楚,配置连接数据库的基本信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///smm" userId="root" password="root"> </jdbcConnection> <!-- 用于指定JDBC类型和Java类型如何转换,最多可以配置一个 forceBigDecimals:控制是否强制将DECIMAL和NUMERIC类型的JDBC字段转换成Java类型的 BigDecimal 默认为false,一般不需要配置 --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- javaModelGenerator:用来控制生成的实体类 targetPackage:生成Model类存放位置(包名) targetProject:指定目标项目路径(根目录) 对应的子属性: trimStrings:判断是否对数据库查询结果进行trim操作(默认false) --> <javaModelGenerator targetPackage="cn.itsource.ssm.wy.domain" targetProject="src\main\java"> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- sqlMapGenerator:生成映射文件存放位置(Mapper.xml文件) targetPackage:生成SQL映射文件(XML文件)在哪个包中 targetProject:指定目标项目路径(根目录) --> <sqlMapGenerator targetPackage="cn.itsource.ssm.wy.mapper" targetProject="src\main\resources"> </sqlMapGenerator> <!-- javaClientGenerator:Java客户端生成器(生成Dao/Mapper的接口) 该 标签可选(最多配置一个),如果不配置,就不会生成Mapper接口 type:选择客户端代码生成器 MyBatis3 ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件 MIXEDMAPPER:XML和注解混合形式 XMLMAPPER:所有方法都在XML中(接口调用依赖XML) MyBatis3Simple ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件 XMLMAPPER:所有方法都在XML中(接口调用依赖XML) targetPackage:生成Mapper接口存放的包名 targetProject:指定目标项目路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.itsource.ssm.wy.mapper" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- table:生成对应表及类名 tableName:对应表名(注:%代表所有) domainObjectName:对应的类名 generatedKey:主键自增的id字段(针对当前 数据库配置MySQL) --> <!-- 多个表:一一写,但是很麻烦的 <table tableName="t_user" domainObjectName="User"> <generatedKey column="id" sqlStatement="MySql" /> </table> <table tableName="t_employee" domainObjectName="Employee"> <generatedKey column="id" sqlStatement="MySql" /> </table>--> <!--所有表: 但是生成的domain名字就不能自定义了. 默认就是数据库表的驼峰式命名. 处理:数据不用t. 自己改源码啊. <table tableName="%" > <generatedKey column="id" sqlStatement="MySql" /> </table>--> <table tableName="employee" domainObjectName="Employee"> <generatedKey column="id" sqlStatement="MySql" /> </table> </context> </generatorConfiguration>
完成以后,自动生成一个表,测试如下
6.controller
@Controller @RequestMapping("/department") public class DepartmentController { @RequestMapping("/list") @ResponseBody//返回json数据 public List<Department> list(){ return departmentService.findAll(); } }
7.测试.访问返回json数据
恭喜:大功告成!如需前台代码,请关注再找,提供EasyUI LayUI Vue三种版本前台代码!