1.首先是建立一个Maven-project
2. 然后导入maven依赖,这里将我的依赖列出如下
4.0.0org.bb ssm war0.0.1-SNAPSHOTssm Maven Webapphttp://maven.apache.orgjunit junit 4.10testorg.springframework spring-context 4.1.3.RELEASEorg.springframework spring-beans 4.1.3.RELEASEorg.springframework spring-webmvc 4.1.3.RELEASEorg.springframework spring-jdbc 4.1.3.RELEASEorg.springframework spring-aspects 4.1.3.RELEASEorg.mybatis mybatis 3.4.2org.mybatis mybatis-spring 1.3.0mysql mysql-connector-Java 5.1.6apache-log4j log4j 1.2.15jstl jstl 1.2javax.servlet servlet-api 2.5providedjavax.servlet jsp-api 2.0providedssmorg.mybatis.generator mybatis-generator-maven-plugin 1.3.2truetrue
3.然后在src/main/java下新建需要的package,如下图所示
![7ef60b02970e771e96eba05986d7a349.png](https://i-blog.csdnimg.cn/blog_migrate/ef7021c476447e7c218faf158b67e0b9.jpeg)
各包的用途如下:controller-无需多说,放的是springmvc的controller文件
mode-放的实体类
service-业务层
service.impl-业务实现层
mapper-mybatis的操作数据库的接口文件,类似于dao层(这里需要注意src/main/resources下的目录,和org.bb.ssm.mapper包名一样)
4.此处我使用mybatis-generator自动生成映射类、dao、xml文件
具体做法是:现在pom.xml中引用mybatis-generator这个插件(前面我们已经引进),在src/main/resource下新建generatorConfig.xml,内容如下
<?xml version="1.0" encoding="UTF-8"?>
5.鼠标右击pom.xml,run as "Maven build" 在goals中输入命令:mybatis-generator:generate,即可自动生成相应文件。
6.下面开始配置spring与mybatis的整合配置--applicationContext-mybatis.xml,直接上配置:
[html] view plain copy<?xml version="1.0" encoding="UTF-8"?>
7.配置Mybatis的配置文件,mybatis官方文档推荐起名为mybatis-config.xml,这里遵循约定:
view plain copy
<?xml version="1.0" encoding="UTF-8" ?>
8.下来是springMVC的配置文件spring-MVC.xml:
<?xml version="1.0" encoding="UTF-8"?>
9.web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> org.springframework.web.context.ContextLoaderListenercontextConfigLocationclasspath:applicationContext-mybatis.xmlspringMVCorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring-MVC.xml1springMVC/org.springframework.web.util.IntrospectorCleanupListenerencodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8forceEncodingtrueencodingFilter/*HiddenHttpMethodFilterorg.springframework.web.filter.HiddenHttpMethodFilterHiddenHttpMethodFilter/*index.jsp
10.log4j日志配置:
### set log levels ### log4j.rootLogger = info , Console , D #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=INFO log4j.logger.java.sql.Statement=INFO log4j.logger.java.sql.PreparedStatement=INFO #output2file log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = D:/logs/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = INFO ## è¾åºinfo级å«ä»¥ä¸çæ¥å¿ log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n 10.补贴上数据库的建表sql:[html] view plain copyCREATE DATABASE /*!32312 IF NOT EXISTS*/`ssm` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `ssm`; /*Table structure for table `userinfo` */ DROP TABLE IF EXISTS `userinfo`; CREATE TABLE `userinfo` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(30) DEFAULT NULL, `user_age` int(3) DEFAULT NULL, `user_address` varchar(400) DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11.配置到这里已经完成,启动项目访问欢迎页面,如果看到熟悉的Hello world 代表整合成功了。
![d768ffe964955c936c8b75e9155f5a0a.png](https://i-blog.csdnimg.cn/blog_migrate/c27daa551e751d98a26a8e2a2933a9ac.jpeg)
想要更多demo可以私信我,私信我:“资料”,可免费领取更多学习资料哦
![2cc7c697381973a61c8170ce0cb4f564.png](https://i-blog.csdnimg.cn/blog_migrate/a2a70f773e7f2a65112223606859f5eb.jpeg)
![93efdd33a9a77970d4ea984b161a8565.png](https://i-blog.csdnimg.cn/blog_migrate/f09bcb0999026a1e9f64b3cfdef7eaef.jpeg)