欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

sm框架 访问局域网mysql_ssm框架搭建之sm框架整合

发布时间:2023/12/20 数据库 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 sm框架 访问局域网mysql_ssm框架搭建之sm框架整合 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

之前已经搭建好了ss框架,现在接上sm框架搭整合;

1 需要引入几个依赖jar包:

org.mybatis

mybatis

3.1.1

org.mybatis

mybatis-spring

1.1.1

2 配置XML文件

1⃣️为了扩展和便于管理,将单独配置操作持久层的文件,一个文件一个数据源

上图中:spring-dal-mysql就是我的持久层的配置文件了,如果需要加载到这个文件可以在applicationContext.xml文件中引入:即可。

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"

default-lazy-init="true">

ps:上面红色标示的参数值(${jdbc.username};${jdbc.password})可是有小技巧的哦。因为这个项目是用maven来管理的,所以下面贴上这个地方的一个小配置,在pom.xml文件中加入:

true

dev

dev

test

test

FistSpringMvcPrj

src/filters/${env}.properties

src/main/java

true

**/*.xml

**/*.properties

**/*.xlsx

src/main/resources

true

**/*.xml

**/*.properties

**/*.xlsx

**/.svn

3 *mapper.xml 文件的配置,本文中叫:UserMapper.xml .但是还需要一个地方需要注意,就是这个 *mapper.xml 中还需要关联好对应的mapper.java文件,“” 这句话。不然的话会报错的,见报错。

代码1:

报错:

15:05:36.343 [RMI TCP Connection(2)-127.0.0.1] ERROR o.m.spring.mapper.MapperFactoryBean - Error while adding the mapper 'interface FirstDemo.dao.mapper.UserMapper' to configuration.

org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 1; 文件提前结束。

at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:253) ~[mybatis-3.1.1.jar:3.1.1]

4 几个坑:

jar包版本不一致一般会出现的错误,比如说这里的,因为spring-core的版本应该和其他spring的包保持一致,但是我这里没有,而是低了几个版本,一般报这个错基本是两个原因:1⃣️jar版本不对,2⃣️缺少对应jar包了:

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) [spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]

没有在*mapper.xml 中配置对应的namespace:

14:42:14.939 [RMI TCP Connection(2)-127.0.0.1] ERROR o.m.spring.mapper.MapperFactoryBean - Error while adding the mapper 'interface FirstDemo.dao.mapper.UserMapper' to configuration.

org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 1; 文件提前结束。

需要加上上面3中红色部门标记的配置,这个制定的是对应的接口的那个mapper.java文件,一般这两个是放在一个包下面的

总结

以上是生活随笔为你收集整理的sm框架 访问局域网mysql_ssm框架搭建之sm框架整合的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。