一 创建Maven项目具体步骤如下:
二 配置文件
2.1 配置mybatis-config.xml
2.2 配置mapper.xml
2.3 配置jdbc.properties
2.4 配置applicationContext.xml
2.5 配置springmvc-servlet.xml
2.6 配置web.xml
2.7 配置log4j.properties
2.8 引入依赖
三 测试
3.1 创建java文件
3.1.1 UserController类
3.1.2 User实体类
3.1.3 UserService接口
3.1.4 UserServiceImpl实现类
3.1.5 UserMapper类
3.2 创建MySql数据库表
3.3 创建users.jsp文件
3.4 结果
==============================================================================
1.设计数据库表如下:
/*
Date: 2019-03-11 23:19:10
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) DEFAULT NULL COMMENT '用户名',
`password` varchar(100) DEFAULT NULL COMMENT '密码',
`name` varchar(100) DEFAULT NULL COMMENT '姓名',
`age` int(10) DEFAULT NULL COMMENT '年龄',
`sex` tinyint(1) DEFAULT NULL COMMENT '性别,1男性,2女性',
`birthday` date DEFAULT NULL COMMENT '出生日期',
`created` datetime DEFAULT NULL COMMENT '创建时间',
`updated` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
INSERT INTO `tb_user` VALUES (1, 'zhangsan', '123456', '张三', 30, 1, '1984-8-8', '2014-9-19 16:56:04', '2014-9-21 11:24:59');
INSERT INTO `tb_user` VALUES (2, 'lisi', '123456', '李四', 21, 2, '1991-1-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');
INSERT INTO `tb_user` VALUES (3, 'wangwu', '123456', '王五', 22, 2, '1989-1-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');
INSERT INTO `tb_user` VALUES (4, 'zhangwei', '123456', '张伟', 20, 1, '1988-9-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');
INSERT INTO `tb_user` VALUES (5, 'lina', '123456', '李娜', 28, 1, '1985-1-1', '2014-9-19 16:56:04', '2014-9-19 16:56:04');
INSERT INTO `tb_user` VALUES (6, 'lilei', '123456', '李磊', 23, 1, '1988-8-8', '2014-9-20 11:41:15', '2014-9-20 11:41:15');
INSERT INTO `tb_user` VALUES (8, 'xiaofeng', '123456', '萧峰', 30, 1, '2018-6-25', '2018-6-25 18:42:25', '2018-6-25 18:42:25');
INSERT INTO `tb_user` VALUES (12, 'zhendeshuai', '565656', '吴彦祖', 26, 1, '2018-6-26', '2018-6-26 14:55:59', '2018-6-26 15:12:12');
INSERT INTO `tb_user` VALUES (14, 'jiumozhi', '123456', '鸠摩智', 30, 1, '2018-6-26', '2018-6-26 23:53:15', '2018-6-26 23:53:15');
INSERT INTO `tb_user` VALUES (21, 'zhangsan1234', '12345', '杰克', 20, 1, '2000-1-1', '2018-7-7 21:50:26', '2018-7-7 21:50:26');
INSERT INTO `tb_user` VALUES (22, 'zhangsanqwe1', '123455', '张三', 30, 1, '1999-2-25', '2018-7-7 21:51:20', '2018-7-7 21:51:20');
INSERT INTO `tb_user` VALUES (25, 'qianjiu', '123456', '钱九', 30, 1, '1989-1-1', '2018-7-7 21:51:20', '2018-7-7 21:51:20');
INSERT INTO `tb_user` VALUES (26, 'zhaoniu', '123456', '赵牛', 28, 1, '1989-1-1', '2018-7-7 21:51:20', '2018-7-7 21:51:20');
2 创建一个ssmDemo项目(项目名称可以自定义),如图所示:
创建好之后如下图所示:
项目会报错,不用急,为什么呢?因为这是一个不完整的maven项目,缺少web.xml。
右键点击项目,如下操作,会自动补全web.xml文件
3,向ssmzh项目中的pom.xml导入spring-springmvc-mybatis整合所需要的依赖,具体依赖如下:
<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.agu</groupId>
<artifactId>SSMDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!--配置webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!--配置spring jdbc,用来加载DataSourceTransactionManager-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!--配置切面的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!--mybatis,用来加载SqlSessionFactoryBean和MapperScannerConfigurer-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!--数据库驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!--日志文件-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
<!--json处理工具包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 连接池,用来加载DruidDataSource-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!--javaScript标签库-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
4,配置spring.xml(企业常用applicationContext.xml命名表示是spring的配置,我这里命名为spring.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--开启spring注解,扫描spring注解所在的包 -->
<context:component-scan base-package="com.usermanage"/>
<!-- 加载资源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 配置资源文件 -->
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!--spring 配置连接池,数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${passwd}"></property>
</bean>
</beans>
5,spring-tx(spring事务的配置)如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 定义事务策略 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--所有以query开头的方法都是只读的 -->
<tx:method name="query*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<!--其他方法使用默认事务策略 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<!--pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型,
这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配cn.itcast.mybatis.service包下的所有类的所有
方法 -->
<aop:pointcut id="myPointcut" expression="execution(* com.usermanage.service.*.*(..))" />
<!--将定义好的事务处理策略应用到上述的切入点 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>
</beans>
6,spring-mybatis.xml整合配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- spring整合Mybatis的配置,其实主要是mybatis的相应配置 -->
<!--
sqlSession工厂 mapper的接口配置
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 加载全局的配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<!-- 配置mapper的扫描,找到所有的mapper.xml映射文件。编码时放开注释,如果放开需要在mapper中写一个mapper也是可以的 -->
<!-- <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property> -->
<!-- 配置类型别名 -->
<property name="typeAliasesPackage" value="com.usermanage.pojo"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置mapper接口所在路径,扫描路径下的所有的mapper接口 如果配置多个mapper的包,使用逗号进行分割
-->
<!--在mybatis当中,dao层与mapper是一样的,只是不同地方叫法不一样,mapper.xml是写sql的,是用来操作数据库的 -->
<property name="basePackage" value="cn.usermanage.dao,cn.usermanage.mapper" />
</bean>
</beans>
7,springmvc.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"
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">
<!-- 配置推荐使用注解驱动,会默认的加载上面的两个 HandlerMapping, HandlerAdapter -->
<mvc:annotation-driven />
<!-- 开启springmvc注解扫描 -->
<context:component-scan base-package="cn.usermanage.controller"></context:component-scan>
<!-- 这个是excelView的加载,原生态ssm不需要,所以这里是可以省略的 -->
<!-- <bean name="excelView" class="cn.usermanage.view.UserExcelView"></bean> -->
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀,这里是请求的路径文件 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<!-- 后缀 ,支持.jsp的请求-->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 以上是原生态的ssm配置 -->
<!-- 配置第二个视图解析器 -->
<!-- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1"></property>
</bean> -->
<!-- 定义文件上传解析器 -->
<!-- <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
设定默认编码
<property name="defaultEncoding" value="UTF-8"></property>
设定文件上传的最大值5MB,5*1024*1024
<property name="maxUploadSize" value="5242880"></property>
</bean> -->
<!-- 解决静态资源被拦截的问题 -->
<!-- <mvc:default-servlet-handler/> -->
</beans>
8,mybatis的配置如下:
<?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>
<settings>
<!-- 开启驼峰匹配 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 分页助手 -->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 数据库方言 -->
<property name="dialect" value="mysql"/>
<!-- 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数 -->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
</plugins>
</configuration>
9,Mybatis-generator的配置(这个需要请先安装mybatis-generator插件,然后即可),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 >
<!-- 数据库jar包驱动路径必须手动加上去 -->
<classPathEntry location="D:\allworkspace\testspace\mybatis-zidong\src\main\resources\mybatis-generator\mysql-connector-java-5.1.25.jar" />
<!--数据库名称 -->
<context id="test" >
<!-- commentGenerator 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 数据库驱动,连接,用户名,密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" userId="root" password="123" />
<!-- pojo实体信息 ,包名称与项目名称-->
<javaModelGenerator targetPackage="com.usermanage.pojo" targetProject="mybatis-zidong/src/main/java" />
<!-- mapper信息-->
<sqlMapGenerator targetPackage="com.usermanage.mapper" targetProject="mybatis-zidong/src/main/java" />
<!-- dao接口 -->
<javaClientGenerator targetPackage="com.usermanage.dao" targetProject="mybatis-zidong/src/main/java" type="XMLMAPPER" />
<!--表名 ,domainObjectName="Teacher"表示与数据库所对应的实体-->
<table schema="teacher" tableName="teacher" domainObjectName="Teacher"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
10.jdbc.properties配置如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
user=root
passwd=123456
11.log4j.properties日志配置如下:
log4j.rootLogger=DEBUG,A1
log4j.logger.org.mybatis=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
12,配置web.xml,具体如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>ssmzh</display-name>
<!-- 加载spring相关配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 这里使用的是以spring*.xml的通配符方式加载配置的 -->
<param-value>classpath:spring/spring*.xml</param-value>
</context-param>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器,以UTF8编码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC -->
<servlet>
<servlet-name>usermanage</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定加载外部的spring-mvc配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!-- 这个可以不配,可以省略 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>usermanage</servlet-name>
<!-- 拦截所有的请求,除了jsp。 /xx.html js css 会 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
————————————————
版权声明:本文为CSDN博主「从放弃到开始」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_30764991/article/details/79816261