先展示文件结构图对工程结构有大致了解:
主要为 ssm-parent (用来管理jar包版本)是每个工程的父工程,ssm-common(用来处理底层数据),ssm-manager(对数据库信息进行操作)
Pojo(bean)->mapper(Dao)->Service->Controller
一、创建工程
1、新建maven project-> ssm-parent 作为所有工程的父工程用来集中定义jar 及其版本信息。
2、建立maven project ->ssm-common 用来编写底层数据处理类
3、建立maven project ->ssm-manager 对数据库进行操作
4、在ssm-manager 中创建子工程模块 建立ssm-manager-pojo
5、重复上述 操作 建立 ssm-manager-mapper、ssm-managerservice
6、在ssm-manager中创建 子工程 ssm-manager-controller ,Packaging 选择 war(表示web工程)
此时工程会报错:需要到工程的 src/main/webapp 下创建 /WEB-INF/web.xml
二、导入工程及jar包依赖
1、parent -> pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>ssm</groupId> 5 <artifactId>ssm-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <packaging>pom</packaging> 8 <!-- 集中定义依赖版本号 --> 9 <properties> 10 <junit.version>4.12</junit.version> 11 <spring.version>4.1.3.RELEASE</spring.version> 12 <mybatis.version>3.2.8</mybatis.version> 13 <mybatis.spring.version>1.2.2</mybatis.spring.version> 14 <mybatis.paginator.version>1.2.15</mybatis.paginator.version> 15 <mysql.version>5.1.32</mysql.version> 16 <slf4j.version>1.6.4</slf4j.version> 17 <jackson.version>2.4.2</jackson.version> 18 <druid.version>1.0.9</druid.version> 19 <httpclient.version>4.3.5</httpclient.version> 20 <jstl.version>1.2</jstl.version> 21 <servlet-api.version>2.5</servlet-api.version> 22 <jsp-api.version>2.0</jsp-api.version> 23 <joda-time.version>2.5</joda-time.version> 24 <commons-lang3.version>3.3.2</commons-lang3.version> 25 <commons-io.version>1.3.2</commons-io.version> 26 <commons-net.version>3.3</commons-net.version> 27 <pagehelper.version>3.4.2-fix</pagehelper.version> 28 <jsqlparser.version>0.9.1</jsqlparser.version> 29 <commons-fileupload.version>1.3.1</commons-fileupload.version> 30 <jedis.version>2.7.2</jedis.version> 31 <solrj.version>4.10.3</solrj.version> 32 33 </properties> 34 <dependencyManagement> 35 <dependencies> 36 <!-- 时间操作组件 --> 37 <dependency> 38 <groupId>joda-time</groupId> 39 <artifactId>joda-time</artifactId> 40 <version>${joda-time.version}</version> 41 </dependency> 42 <!-- Apache工具组件 --> 43 <dependency> 44 <groupId>org.apache.commons</groupId> 45 <artifactId>commons-lang3</artifactId> 46 <version>${commons-lang3.version}</version> 47 </dependency> 48 <dependency> 49 <groupId>org.apache.commons</groupId> 50 <artifactId>commons-io</artifactId> 51 <version>${commons-io.version}</version> 52 </dependency> 53 <dependency> 54 <groupId>commons-net</groupId> 55 <artifactId>commons-net</artifactId> 56 <version>${commons-net.version}</version> 57 </dependency> 58 <!-- Jackson Json处理工具包 --> 59 <dependency> 60 <groupId>com.fasterxml.jackson.core</groupId> 61 <artifactId>jackson-databind</artifactId> 62 <version>${jackson.version}</version> 63 </dependency> 64 <!-- httpclient --> 65 <dependency> 66 <groupId>org.apache.httpcomponents</groupId> 67 <artifactId>httpclient</artifactId> 68 <version>${httpclient.version}</version> 69 </dependency> 70 <!-- 单元测试 --> 71 <dependency> 72 <groupId>junit</groupId> 73 <artifactId>junit</artifactId> 74 <version>${junit.version}</version> 75 <scope>test</scope> 76 </dependency> 77 <!-- 日志处理 --> 78 <dependency> 79 <groupId>org.slf4j</groupId> 80 <artifactId>slf4j-log4j12</artifactId> 81 <version>${slf4j.version}</version> 82 </dependency> 83 <!-- Mybatis --> 84 <dependency> 85 <groupId>org.mybatis</groupId> 86 <artifactId>mybatis</artifactId> 87 <version>${mybatis.version}</version> 88 </dependency> 89 <dependency> 90 <groupId>org.mybatis</groupId> 91 <artifactId>mybatis-spring</artifactId> 92 <version>${mybatis.spring.version}</version> 93 </dependency> 94 <dependency> 95 <groupId>com.github.miemiedev</groupId> 96 <artifactId>mybatis-paginator</artifactId> 97 <version>${mybatis.paginator.version}</version> 98 </dependency> 99 <dependency> 100 <groupId>com.github.pagehelper</groupId> 101 <artifactId>pagehelper</artifactId> 102 <version>${pagehelper.version}</version> 103 </dependency> 104 <!-- MySql --> 105 <dependency> 106 <groupId>mysql</groupId> 107 <artifactId>mysql-connector-java</artifactId> 108 <version>${mysql.version}</version> 109 </dependency> 110 <!-- 连接池 --> 111 <dependency> 112 <groupId>com.alibaba</groupId> 113 <artifactId>druid</artifactId> 114 <version>${druid.version}</version> 115 </dependency> 116 <!-- Spring --> 117 <dependency> 118 <groupId>org.springframework</groupId> 119 <artifactId>spring-context</artifactId> 120 <version>${spring.version}</version> 121 </dependency> 122 <dependency> 123 <groupId>org.springframework</groupId> 124 <artifactId>spring-beans</artifactId> 125 <version>${spring.version}</version> 126 </dependency> 127 <dependency> 128 <groupId>org.springframework</groupId> 129 <artifactId>spring-webmvc</artifactId> 130 <version>${spring.version}</version> 131 </dependency> 132 <dependency> 133 <groupId>org.springframework</groupId> 134 <artifactId>spring-jdbc</artifactId> 135 <version>${spring.version}</version> 136 </dependency> 137 <dependency> 138 <groupId>org.springframework</groupId> 139 <artifactId>spring-aspects</artifactId> 140 <version>${spring.version}</version> 141 </dependency> 142 <!-- JSP相关 --> 143 <dependency> 144 <groupId>jstl</groupId> 145 <artifactId>jstl</artifactId> 146 <version>${jstl.version}</version> 147 </dependency> 148 <dependency> 149 <groupId>javax.servlet</groupId> 150 <artifactId>servlet-api</artifactId> 151 <version>${servlet-api.version}</version> 152 <scope>provided</scope> 153 </dependency> 154 <dependency> 155 <groupId>javax.servlet</groupId> 156 <artifactId>jsp-api</artifactId> 157 <version>${jsp-api.version}</version> 158 <scope>provided</scope> 159 </dependency> 160 <!-- 文件上传组件 --> 161 <dependency> 162 <groupId>commons-fileupload</groupId> 163 <artifactId>commons-fileupload</artifactId> 164 <version>${commons-fileupload.version}</version> 165 </dependency> 166 <!-- Redis客户端 --> 167 <dependency> 168 <groupId>redis.clients</groupId> 169 <artifactId>jedis</artifactId> 170 <version>${jedis.version}</version> 171 </dependency> 172 <!-- solr客户端 --> 173 <dependency> 174 <groupId>org.apache.solr</groupId> 175 <artifactId>solr-solrj</artifactId> 176 <version>${solrj.version}</version> 177 </dependency> 178 </dependencies> 179 </dependencyManagement> 180 181 <build> 182 <finalName>${project.artifactId}</finalName> 183 <plugins> 184 <!-- 资源文件拷贝插件 --> 185 <plugin> 186 <groupId>org.apache.maven.plugins</groupId> 187 <artifactId>maven-resources-plugin</artifactId> 188 <version>2.7</version> 189 <configuration> 190 <encoding>UTF-8</encoding> 191 </configuration> 192 </plugin> 193 <!-- java编译插件 --> 194 <plugin> 195 <groupId>org.apache.maven.plugins</groupId> 196 <artifactId>maven-compiler-plugin</artifactId> 197 <version>3.2</version> 198 <configuration> 199 <source>1.7</source> 200 <target>1.7</target> 201 <encoding>UTF-8</encoding> 202 </configuration> 203 </plugin> 204 </plugins> 205 <pluginManagement> 206 <plugins> 207 <!-- 配置Tomcat插件 --> 208 <plugin> 209 <groupId>org.apache.tomcat.maven</groupId> 210 <artifactId>tomcat7-maven-plugin</artifactId> 211 <version>2.2</version> 212 </plugin> 213 </plugins> 214 </pluginManagement> 215 </build> 216 217 </project>
2、common -> pom.xml 导入jar 依赖
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <parent> 4 <groupId>ssm</groupId> 5 <artifactId>ssm-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <groupId>ssm</groupId> 9 <artifactId>ssm-common</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <dependencies> 12 <!-- 时间操作组件 --> 13 <dependency> 14 <groupId>joda-time</groupId> 15 <artifactId>joda-time</artifactId> 16 </dependency> 17 <!-- Apache工具组件 --> 18 <dependency> 19 <groupId>org.apache.commons</groupId> 20 <artifactId>commons-lang3</artifactId> 21 </dependency> 22 <dependency> 23 <groupId>org.apache.commons</groupId> 24 <artifactId>commons-io</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>commons-net</groupId> 28 <artifactId>commons-net</artifactId> 29 </dependency> 30 <!-- Jackson Json处理工具包 --> 31 <dependency> 32 <groupId>com.fasterxml.jackson.core</groupId> 33 <artifactId>jackson-databind</artifactId> 34 </dependency> 35 <!-- httpclient --> 36 <dependency> 37 <groupId>org.apache.httpcomponents</groupId> 38 <artifactId>httpclient</artifactId> 39 </dependency> 40 <!-- 单元测试 --> 41 <dependency> 42 <groupId>junit</groupId> 43 <artifactId>junit</artifactId> 44 <scope>test</scope> 45 </dependency> 46 47 <!-- 日志处理 --> 48 <dependency> 49 <groupId>org.slf4j</groupId> 50 <artifactId>slf4j-log4j12</artifactId> 51 </dependency> 52 </dependencies> 53 </project>
3、ssm-manager -> pom.xml (1)导入工程依赖(2)添加tomcat插件
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <parent> 4 <groupId>ssm</groupId> 5 <artifactId>ssm-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <groupId>ssm</groupId> 9 <artifactId>ssm-manager</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <packaging>pom</packaging> 12 <dependencies> 13 <!-- 依赖于common工程 --> 14 <dependency> 15 <groupId>ssm</groupId> 16 <artifactId>ssm-common</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </dependency> 19 </dependencies> 20 <modules> 21 <module>ssm-manager-pojo</module> 22 <module>ssm-manager-mapper</module> 23 <module>ssm-manager-service</module> 24 <module>ssm-manager-controller</module> 25 </modules> 26 <build> 27 <plugins> 28 <!-- Tomcat 插件 --> 29 <plugin> 30 <groupId>org.apache.tomcat.maven</groupId> 31 <artifactId>tomcat7-maven-plugin</artifactId> 32 <configuration> 33 <port>8080</port> 34 <path>/</path> 35 </configuration> 36 </plugin> 37 </plugins> 38 </build> 39 </project>
4、ssm-manger-pojo -> pom.xml (1)导入jar包依赖(2)导入工程依赖
<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> <parent> <groupId>ssm</groupId> <artifactId>ssm-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>ssm</groupId> <artifactId>ssm-manager-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>ssm</groupId> <artifactId>ssm-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
5、ssm-manger-mapper-> pom.xml (1)导入jar包依赖(2)导入工程依赖
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <parent> 5 <groupId>ssm</groupId> 6 <artifactId>ssm-manager</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 </parent> 9 <groupId>ssm</groupId> 10 <artifactId>ssm-manager-mapper</artifactId> 11 <version>0.0.1-SNAPSHOT</version> 12 <dependencies> 13 <dependency> 14 <groupId>ssm</groupId> 15 <artifactId>ssm-manager-pojo</artifactId> 16 <version>0.0.1-SNAPSHOT</version> 17 </dependency> 18 19 <!-- Mybatis --> 20 <dependency> 21 <groupId>org.mybatis</groupId> 22 <artifactId>mybatis</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.mybatis</groupId> 26 <artifactId>mybatis-spring</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>com.github.miemiedev</groupId> 30 <artifactId>mybatis-paginator</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>com.github.pagehelper</groupId> 34 <artifactId>pagehelper</artifactId> 35 </dependency> 36 <!-- MySql --> 37 <dependency> 38 <groupId>mysql</groupId> 39 <artifactId>mysql-connector-java</artifactId> 40 </dependency> 41 <!-- 连接池 --> 42 <dependency> 43 <groupId>com.alibaba</groupId> 44 <artifactId>druid</artifactId> 45 </dependency> 46 </dependencies> 47 <build> 48 <!-- 配置将mybatis的mapper.xml不会复制进web工程,会报错绑定异常找不到mapper.xml --> 49 <resources> 50 <resource> 51 <directory>src/main/java</directory> 52 <includes> 53 <include>**/*.properties</include> 54 <include>**/*.xml</include> 55 </includes> 56 <filtering>false</filtering> 57 </resource> 58 </resources> 59 </build> 60 </project>
5、ssm-manger-service-> pom.xml (1)导入jar包依赖(2)导入工程依赖
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <parent> 4 <groupId>ssm</groupId> 5 <artifactId>ssm-manager</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <groupId>ssm</groupId> 9 <artifactId>ssm-manager-service</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <dependencies> 12 <dependency> 13 <groupId>ssm</groupId> 14 <artifactId>ssm-manager-mapper</artifactId> 15 <version>0.0.1-SNAPSHOT</version> 16 </dependency> 17 <!-- Spring --> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-context</artifactId> 21 </dependency> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-beans</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-webmvc</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-jdbc</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-aspects</artifactId> 37 </dependency> 38 </dependencies> 39 </project>
6、ssm-manger-controller-> pom.xml (1)导入jar包依赖(2)导入工程依赖
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <parent> 4 <groupId>ssm</groupId> 5 <artifactId>ssm-manager</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <groupId>ssm</groupId> 9 <artifactId>ssm-manager-controller</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <packaging>war</packaging> 12 <dependencies> 13 <dependency> 14 <groupId>ssm</groupId> 15 <artifactId>ssm-manager-service</artifactId> 16 <version>0.0.1-SNAPSHOT</version> 17 </dependency> 18 <!-- JSP相关 --> 19 <dependency> 20 <groupId>jstl</groupId> 21 <artifactId>jstl</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>javax.servlet</groupId> 25 <artifactId>servlet-api</artifactId> 26 <scope>provided</scope> 27 </dependency> 28 <dependency> 29 <groupId>javax.servlet</groupId> 30 <artifactId>jsp-api</artifactId> 31 <scope>provided</scope> 32 </dependency> 33 <!-- 文件上传组件 --> 34 <dependency> 35 <groupId>commons-fileupload</groupId> 36 <artifactId>commons-fileupload</artifactId> 37 </dependency> 38 <!-- junit依赖 --> 39 <dependency> 40 <groupId>org.hamcrest</groupId> 41 <artifactId>hamcrest-core</artifactId> 42 <version>1.3</version> 43 </dependency> 44 </dependencies> 45 </project>
三、SSM整合(全部在controller层)
1、编写propertiees 文件
db.properties
1 jdbc.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8
2 jdbc.username = root
3 jdbc.password = 000000
4 jdbc.driver =com.mysql.jdbc.Driver
log4j.properties
1 # Global logging configuration
2 log4j.rootLogger=ERROR, stdout
3 # MyBatis logging configuration...
4 log4j.logger.org.mybatis.example.BlogMapper=TRACE
5 # Console output...
6 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
7 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2、编写mybatis-conf.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 </configuration>
3、编写springMVC.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xmlns:util="http://www.springframework.org/schema/util" 8 xmlns:context="http://www.springframework.org/schema/context" 9 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 10 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 11 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 14 <!-- 扫描包 --> 15 <context:component-scan base-package="com.controller"></context:component-scan> 16 <!-- 配置--> 17 <mvc:annotation-driven></mvc:annotation-driven> 18 <!-- 静态资源映射 --> 19 <mvc:resources location="/WEB-INF/css" mapping="/css/**"/> 20 <mvc:resources location="/WEB-INF/js" mapping="/js/**"/> 21 <!--<mvc:resources location="/WEB-INF/" mapping="/css/**"/> 22 --> <!-- <mvc:default-servlet-handler/> --> 23 <!-- 视图解析器 --> 24 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 25 <property name="prefix" value="/WEB-INF/jsp"></property> 26 <property name="suffix" value=".jsp"></property> 27 </bean> 28 </beans>
4、编写spring配置文件
applicationContext-mapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 7 xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 8 xmlns:util="http://www.springframework.org/schema/util" 9 xmlns:tx="http://www.springframework.org/schema/tx" 10 xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 11 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 14 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 16 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 17 18 <!-- 数据库连接池 --> 19 <!-- 加载配置文件 --> 20 <context:property-placeholder location="classpath:/resource/db.properties"/> 21 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> 22 <property name="url" value="${jdbc.url}"></property> 23 <property name="username" value="${jdbc.username}"></property> 24 <property name="password" value="${jdbc.password}"></property> 25 <property name="driverClassName" value="${jdbc.driver}"></property> 26 <property name="maxActive" value="10"></property> 27 <property name="minIdle" value="5"></property> 28 </bean> 29 <!-- 让spring管理sqlsessionfactory 使用 mybatis 和 spring 整合包 --> 30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <!-- 数据库连接池 --> 32 <property name="dataSource" ref="dataSource"></property> 33 <!-- 加载mybatis 的全局属性 --> 34 <property name="configLocation" value="classpath:mybatis/mybatis-conf.xml"></property> 35 </bean> 36 <!-- 配置扫描包,加载mapper代理对象 mybatis与spring整合包在spring配置文件中扫描mapper接口 --> 37 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 38 <property name="basePackage" value="com.mapper"></property> 39 </bean> 40 </beans>
applicationContext-service.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 7 <!-- 扫描包 --> 8 <context:component-scan base-package="com.service"></context:component-scan> 9 </beans>
applicationContext-trans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:util="http://www.springframework.org/schema/util" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 11 <!-- 配置事务管理器 --> 12 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 13 <!-- 数据源 --> 14 <property name="dataSource" ref="dataSource"></property> 15 </bean> 16 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 17 <tx:attributes> 18 <!-- 传播行为 --> 19 <tx:method name="save*" propagation="REQUIRED"/> 20 <tx:method name="insert*" propagation="REQUIRED"/> 21 <tx:method name="add*" propagation="REQUIRED"/> 22 <tx:method name="create*" propagation="REQUIRED"/> 23 <tx:method name="delete*" propagation="REQUIRED"/> 24 <tx:method name="update*" propagation="REQUIRED"/> 25 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> 26 <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> 27 <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> 28 </tx:attributes> 29 </tx:advice> 30 <!-- 切面 --> 31 <aop:config> 32 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.*.*(..))"/> 33 </aop:config> 34 </beans>
web.xml中注册spring 、springMVC (web.xml的位置:controller>src>main>webapp>WEB-INF>web.xml 目录文件不自动创建,需要手动创建)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 <display-name>ssm-web</display-name> 7 <welcome-file-list> 8 <welcome-file>index.jsp</welcome-file> 9 </welcome-file-list> 10 <!-- 加载spring容器 --> 11 <context-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:spring/applicationContext*.xml</param-value> 14 </context-param> 15 <listener> 16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 17 </listener> 18 19 <!-- 解决post乱码 --> 20 <filter> 21 <filter-name>CharacterEncodingFilter</filter-name> 22 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 23 <init-param> 24 <param-name>encoding</param-name> 25 <param-value>utf-8</param-value> 26 </init-param> 27 <!-- <init-param> 28 <param-name>forceEncoding</param-name> 29 <param-value>true</param-value> 30 </init-param> --> 31 </filter> 32 <filter-mapping> 33 <filter-name>CharacterEncodingFilter</filter-name> 34 <url-pattern>/*</url-pattern> 35 </filter-mapping> 36 37 <!-- springmvc的前端控制器 --> 38 <servlet> 39 <servlet-name>DispatcherServlet</servlet-name> 40 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 41 <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --> 42 <init-param> 43 <param-name>contextConfigLocation</param-name> 44 <param-value>classpath:spring/springMVC.xml</param-value> 45 </init-param> 46 <load-on-startup>1</load-on-startup> 47 </servlet> 48 <servlet-mapping> 49 <servlet-name>DispatcherServlet</servlet-name> 50 <url-pattern>/</url-pattern> 51 </servlet-mapping> 52 </web-app>
四、创建查询
1、ssm-common 编写 SsmResulat.java 用来处理查询结果转json发送
1 package com.common; 2 import java.util.List; 3 import com.fasterxml.jackson.databind.JsonNode; 4 import com.fasterxml.jackson.databind.ObjectMapper; 5 public class SsmResult { 6 // 定义jackson对象 7 private static final ObjectMapper MAPPER = new ObjectMapper(); 8 // 响应业务状态 9 private Integer status; 10 // 响应消息 11 private String msg; 12 // 响应中的数据 13 private Object data; 14 public static SsmResult build(Integer status, String msg, Object data) { 15 return new SsmResult(status, msg, data); 16 } 17 18 public static SsmResult ok(Object data) { 19 return new SsmResult(data); 20 } 21 22 public static SsmResult ok() { 23 return new SsmResult(null); 24 } 25 public SsmResult() { 26 } 27 public static SsmResult build(Integer status, String msg) { 28 return new SsmResult(status, msg, null); 29 } 30 public SsmResult(Integer status, String msg, Object data) { 31 this.status = status; 32 this.msg = msg; 33 this.data = data; 34 } 35 36 public SsmResult(Object data) { 37 this.status = 200; 38 this.msg = "OK"; 39 this.data = data; 40 } 41 42 // public Boolean isOK() { 43 // return this.status == 200; 44 // } 45 46 public Integer getStatus() { 47 return status; 48 } 49 public void setStatus(Integer status) { 50 this.status = status; 51 } 52 public String getMsg() { 53 return msg; 54 } 55 public void setMsg(String msg) { 56 this.msg = msg; 57 } 58 public Object getData() { 59 return data; 60 } 61 public void setData(Object data) { 62 this.data = data; 63 } 64 /** 65 * 将json结果集转化为SsmResult对象 66 * 67 * @param jsonData json数据 68 * @param clazz SsmResult中的object类型 69 * @return 70 */ 71 public static SsmResult formatToPojo(String jsonData, Class<?> clazz) { 72 try { 73 if (clazz == null) { 74 return MAPPER.readValue(jsonData, SsmResult.class); 75 } 76 JsonNode jsonNode = MAPPER.readTree(jsonData); 77 JsonNode data = jsonNode.get("data"); 78 Object obj = null; 79 if (clazz != null) { 80 if (data.isObject()) { 81 obj = MAPPER.readValue(data.traverse(), clazz); 82 } else if (data.isTextual()) { 83 obj = MAPPER.readValue(data.asText(), clazz); 84 } 85 } 86 return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); 87 } catch (Exception e) { 88 return null; 89 } 90 } 91 92 /** 93 * 没有object对象的转化 94 * 95 * @param json 96 * @return 97 */ 98 public static SsmResult format(String json) { 99 try { 100 return MAPPER.readValue(json, SsmResult.class); 101 } catch (Exception e) { 102 e.printStackTrace(); 103 } 104 return null; 105 } 106 107 /** 108 * Object是集合转化 109 * 110 * @param jsonData json数据 111 * @param clazz 集合中的类型 112 * @return 113 */ 114 public static SsmResult formatToList(String jsonData, Class<?> clazz) { 115 try { 116 JsonNode jsonNode = MAPPER.readTree(jsonData); 117 JsonNode data = jsonNode.get("data"); 118 Object obj = null; 119 if (data.isArray() && data.size() > 0) { 120 obj = MAPPER.readValue(data.traverse(), 121 MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); 122 } 123 return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); 124 } catch (Exception e) { 125 return null; 126 } 127 } 128 }
2、用逆向生成mybatis的代码:
mbg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 <!-- 7 targetRuntime="MyBatis3Simple":生成简单版的CRUD 8 MyBatis3:豪华版 9 --> 10 <context id="DB2Tables" targetRuntime="MyBatis3"> 11 <commentGenerator> 12 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 13 <property name="suppressAllComments" value="true" /> 14 </commentGenerator> 15 <!-- jdbcConnection:指定如何连接到目标数据库 --> 16 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 17 connectionURL="jdbc:mysql://localhost:3306/test?allowMultiQueries=true" 18 userId="root" 19 password="000000"> 20 </jdbcConnection> 21 <!-- --> 22 <javaTypeResolver > 23 <property name="forceBigDecimals" value="false" /> 24 </javaTypeResolver> 25 26 <!-- javaModelGenerator:指定javaBean的生成策略 27 targetPackage="test.model":目标包名 28 targetProject="MBGTestProjectsrc":目标工程 29 --> 30 <javaModelGenerator targetPackage="com.pojo" 31 targetProject=".src"> 32 <property name="enableSubPackages" value="false" /> 33 <property name="trimStrings" value="true" /> 34 </javaModelGenerator> 35 <!-- sqlMapGenerator:sql映射生成策略: --> 36 <sqlMapGenerator targetPackage="com.mapper" 37 targetProject=".src"> 38 <property name="enableSubPackages" value="false" /> 39 </sqlMapGenerator> 40 <!-- javaClientGenerator:指定mapper接口所在的位置 --> 41 <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" 42 targetProject=".src"> 43 <property name="enableSubPackages" value="false" /> 44 </javaClientGenerator> 45 <!-- 指定要逆向分析哪些表:根据表要创建javaBean --><!-- tableName取 % 创建 数据库下所有--> 46 <table schema="" tableName="account"></table> 47 </context> 48 </generatorConfiguration>
TestMbg.java
1 @Test 2 public void testMbg() throws Exception { 3 List<String> warnings = new ArrayList<String>(); 4 boolean overwrite = true; 5 File configFile = new File("mbg.xml"); 6 ConfigurationParser cp = new ConfigurationParser(warnings); 7 Configuration config = cp.parseConfiguration(configFile); 8 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 9 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, 10 callback, warnings); 11 myBatisGenerator.generate(null); 12 }
生成目录结构如下:
3、将逆向生成的代码复制到 ssm-manager-pojo 、ssm-manager-mapper
4、创建service层java类,并做注解加载进spring容器
AccountService.java
1 package com.service; 2 3 import java.util.List; 4 5 import com.pojo.Account; 6 7 public interface AccountService { 8 public Account getAccount (int id) ; 9 public List<Account> getAccountAll () ; 10 }
AccountServiceImpl.java
1 package com.service; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.mapper.AccountMapper; 9 import com.pojo.Account; 10 import com.pojo.AccountExample; 11 import com.pojo.AccountExample.Criteria; 12 @Service 13 public class AccountServiceImpl implements AccountService{ 14 @Autowired 15 private AccountMapper accountMapper; 16 @Override 17 public List<Account> getAccountAll() { 18 AccountExample accountExample = new AccountExample(); 19 Criteria criteria = accountExample.createCriteria(); 20 criteria.andIdIsNotNull(); 21 List<Account> accountList = accountMapper.selectByExample(accountExample); 22 for (Account account : accountList) { 23 account.toString(); 24 } 25 return accountList; 26 } 27 @Override 28 public Account getAccount(int id) { 29 return null; 30 } 31 32 }
5、编写controller层拦截请求
1 package com.controller; 2 3 import static org.hamcrest.CoreMatchers.nullValue; 4 5 import java.util.List; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.ResponseBody; 10 import com.alibaba.druid.support.json.JSONParser; 11 import com.alibaba.druid.support.json.JSONUtils; 12 import com.common.SsmResult; 13 import com.pojo.Account; 14 import com.service.AccountService; 15 import com.service.AccountServiceImpl; 16 @Controller 17 public class AccountContraller { 18 @Autowired 19 private AccountService accountServiceImpl; 20 @RequestMapping("getAccount") 21 @ResponseBody 22 public SsmResult getAccount() { 23 List<Account> list = accountServiceImpl.getAccountAll(); 24 return SsmResult.ok(list); 25 } 26 }
6、编写页面发送请求:
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>hello</title> 8 </head> 9 <body> 10 maven ssm project<br> 11 <a href="getAccount">Account </a> 12 </body> 13 </html>
五、运行工程
1、安装maven工程
2、运行工程 ssm-manager -> run as -> Mavenbuild (clean tomcat7:run)
3、 访问页面: http://localhost:8080/
点击链接: