使用Maven搭建 SSM 环境
SSM需要的基础jar包有哪些??整理一下:
- c3p0数据连接池
- springMVC的 JSON
- springAOP
- springCore
- springPersitence
- springWeb
- myBatis
- myBatisSpring
- mysql
- mybatis 逆向工程 插件
2017/10/24 16:00:31
我去的是:这个网站中找jar包, 大多数使用最新的jar包
- 本来是使用spring5来进行整合的,结果与asm冲突了,网上又没什么解决方案。
- 最后妥协了改成3.2.2
pom .xml文件配置
pom.xml
如果觉得下载太慢的话,我们可以使用阿里云的私服。只要在settings下配置如下代码即可:
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url> </mirror>
创建mysql用户和数据库
mysql用户:
- 用户名:xxxx
- 密码:xxxx
数据库:
- xxxx
mybatis配置文件
myBatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"">
<configuration>
<!-- 通过别名简化对类的使用 -->
<typeAliases>
<package name="zhongfucheng.entity"/>
</typeAliases>
</configuration>
application-context配置文件
配置spring-mvc
spring-mvc.xml
逆向生成的配置文件
generatorConfig.xml
web.xml配置
web.xml
log4j 配置文件
log4j.properties
测试逆向工程
不知道是不是我的mysql驱动包太高了,出现了如下的问题:
The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
于是乎我就去找到了解决方案,只要在url后面加上时区就好了:
jdbc:mysql:// localhost :3306/zhongfucheng?serverTimezone=UTC
随后又出现了下面的问题:
Column name pattern can not be NULL or empty
找到的解决方案,又是往后面加参数:
jdbc:mysql://localhost:3306/zhongfucheng?serverTimezone=UTC&useSSL=false&nullNamePatternMatchesAll=true
最终就成功了…
…还有问题:在刚开始的时候没注意到: Mybatis Generator 生成的mapper只有insert方法
链接:
又去查了一大堆的资料,发现还是因为mysql的驱动包太高的问题。只能往下转了。
Failed to read candidate component class错误分析:
spring 3.2不支持1.8编译环境,解决办法就是降为1.7编译环境
解决连接:
编写BaseMapper
编写BaseService
BaseServiceImpl
测试Spring和Mybatis是否整合完成
编写BaseAction
/** * 将所有的service实现都定义出来,controller子类使用的使用就不需要定义了。 */ public class BaseController { @Autowired protected StoreHouseService storeHoseService; }
StoreHouseController
@Controller @RequestMapping("/store") public class StoreHouseController extends BaseController { @RequestMapping("/getById.do") public void getById(String id ) { StoreHouse storeHouse = storeHoseService.selectByPrimaryKey(id); System.out.println(storeHouse); } }
测试spring-mvc
在测试的时候出现了: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)的异常。
上网上查看之后,****。因此我就将StoreHouseMapper.xml放到resouces下了。
同时,我们的application-context.xml加载配置文件和generator.xml逆向配置文件的位置也要修改。
说要把生成出来的mapper.xml放在resouces目录下才能识别出来
<!--配置扫描式加载SQL映射文件,那么在mybatis配置文件中就不用加载了--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <!-- 所生成的sqlMap的影射文件的位置,默认资源包src --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
测试JSON
测试代码:
@RequestMapping("/getById.do") @ResponseBody public Object getById(String id ) { StoreHouse storeHouse = storeHoseService.selectByPrimaryKey(id); System.out.println(storeHouse); return storeHouse; }
开发环境总结
到目前为止、SSM已整合完成。
创建web常用的目录结构。
- Intellij idea
- Spring 3.2.2
- Mybatis3.4.5
- mysql 5.1.34
- Tomcat 7.0
- jdk1.7
花了一个晚上总结出:不能太装逼去玩新的jar包、资料太少了,非常容易出错!
想用新版本的jar包遇到了很多的麻烦
- 使用spring5.0与asm发生冲突,无法启动Tomcat,降为3.2.2
- 使用Mysql 6.0在逆向工程的时候出现时区、无法创建表的问题,经过一轮解决,最后生成的SqlMapper只有insert语句。随后降为5.1.34
- 使用JDK1.8有提示过时的错误,后来在pom加入一段配置解决。
- 在初始化Spring容器的时候,无法创建对象。Spring3.2.2与JDK1.8发生冲突。随后降为JDK1.7
- 在Intellij idea降JDK版本需要指定4个部分,详情可参见上面的连接。