maven整合ssm
在做maven整合ssm的时候,现在只是做到mybatis和spring整合,报找不到实体类异常
错误信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in file [E:项目空间eclipseusersCenterusersCenter_commontargetclassesspringapplicationContext_common.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [E:项目空间eclipseusersCenterusersCenter_commontargetclassesspringapplicationContext_common.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis.cfg.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'admins'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.bjsxt.userscenter.users.pojo.Admins at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:563) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.bjsxt.userscenter.common.test.BaseTest.init(BaseTest.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [E:项目空间eclipseusersCenterusersCenter_commontargetclassesspringapplicationContext_common.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis.cfg.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'admins'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.bjsxt.userscenter.users.pojo.Admins at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1589) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByName(AbstractAutowireCapableBeanFactory.java:1255) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1205) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552) ... 35 more Caused by: org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis.cfg.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'admins'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.bjsxt.userscenter.users.pojo.Admins at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:500) at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:380) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585) ... 44 more Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'admins'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.bjsxt.userscenter.users.pojo.Admins at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98) at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:494) ... 47 more Caused by: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'admins'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.bjsxt.userscenter.users.pojo.Admins at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:170) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:108) ... 49 more Caused by: java.lang.ClassNotFoundException: Cannot find class: com.bjsxt.userscenter.users.pojo.Admins at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200) at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89) at org.apache.ibatis.io.Resources.classForName(Resources.java:261) at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:163) ... 50 more java.lang.NullPointerException at com.bjsxt.userscenter.common.test.BaseTest.close(BaseTest.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.bjsxt.userscenter.users.dao.IAdminsDao"> <!-- 添加一条记录 --> <insert id="save" parameterType="admins" useGeneratedKeys="true" keyProperty="id"> insert into a_admins (roleId, email, password, status, trueName, phone, qq, loginFailedCount, loginFailedTime, createTime, updateTime, lastLoginTime) values (#{roleId},#{ email},#{ password},#{ status},#{ trueName},#{ phone},#{ qq},#{ loginFailedCount},#{ loginFailedTime}, #{createTime},#{updateTime},#{lastLoginTime}) </insert> <!-- 更新一条记录 --> <update id="update" parameterType="admins"> update a_admins <set> (roleId=#{roleId}, email=#{email}, password=#{password}, status=#{status}, trueName=#{trueName}, phone=#{phone}, qq=#{qq}, loginFailedCount=#{loginFailedCount}, loginFailedTime=#{loginFailedTime}, createTime=#{createTime}, updateTime=#{updateTime}, lastLoginTime=#{lastLoginTime}) </set> where id = #{id} </update> <!-- 删除一条记录 --> <delete id="delete" parameterType="admins"> delete from a_admins <where> id = #{id} </where> </delete> <!-- 查询一条记录 --> <select id="findOne" parameterType="map" resultType="admins"> select * from a_admins <where> <if test="id != null"> and id = #{id} </if> </where> </select> <!-- 查询多条记录 带分页 --> <!-- pageMarking == true 表示查询总记录数 pageMarking == false 表示使用分页查询 pageMarking == 什么也不传 表示不使用分页直接查询 --> <select id="findCondlist" parameterType="map" resultType="admins"> <choose> <when test="pageMarking =='true'"> <!-- 使用别名,用于储存总的记录数 --> select count(*) as id from a_admins </when> <otherwise> select * from a_admins </otherwise> </choose> <!-- 查询条件 --> <where> <!-- 按照关键字查询 --> <if test="keyword != null and keyword !=''"> and (email like #{keyword} or trueName like #{keyword} or phone like #{keyword} or qq #{keyword}) </if> <!-- 按照状态查询 --> <if test="status != null"> and status = #{status} </if> <!-- 按照时间查询 --> <if test="stDate != null and edDate != null"> <![CDATA[ and updateTime >= #{stDate} and updateTime < #{edDate} ]]> </if> </where> <!-- 排序 --> <choose> <when test="pageMarking == 'true'"/> <otherwise> <choose> <when test="orderby == createTimeASC"> order by createTime asc </when> <otherwise> order by lastLoginTime desc </otherwise> </choose> </otherwise> </choose> <!-- 分页 --> <choose> <when test="pageMarking == 'true'"/> <when test="pageMarking == 'false'"> limit #{currentRecord},#{pageSize} </when> </choose> </select> </mapper>
package com.bjsxt.userscenter.common.test; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.userscenter.common.util.ConstatFinalUtil; /** * 所有测试类的父类 * @author * */ public class BaseTest { protected ApplicationContext ac; /** * 初始化 */ @Before public void init() { ac = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext_*.xml"); ConstatFinalUtil.LOGGER.info("========init==========ac:{}",ac); } /** * 测试方法 */ @Test public void test() { ConstatFinalUtil.LOGGER.info("===========test==========="); } /** * 关闭方法 */ @After public void close() { ClassPathXmlApplicationContext cpxac = (ClassPathXmlApplicationContext) this.ac; cpxac.close(); ConstatFinalUtil.LOGGER.info("===========close==========="); } }
package com.bjsxt.userscenter.users.test; import java.util.Date; import org.junit.Before; import org.junit.Test; import com.alibaba.fastjson.JSONObject; import com.bjsxt.userscenter.common.test.BaseTest; import com.bjsxt.userscenter.common.util.ConstatFinalUtil; import com.bjsxt.userscenter.common.util.EncryptUtil; import com.bjsxt.userscenter.users.pojo.Admins; import com.bjsxt.userscenter.users.service.IUsersService; /** * 用户service相关的测试类 * @author * */ public class UsersServiceTest extends BaseTest { private IUsersService usersService; @Before public void init() { super.init(); this.usersService = (IUsersService) this.ac.getBean("usersService"); } /** * 保存一条管理员记录 */ @Test public void saveOneAdminsService() { Admins admins = new Admins(); admins.setEmail("11"); admins.setPassword(EncryptUtil.encodeStr("123456")); admins.setCreateTime(new Date()); admins.setUpdateTime(new Date()); admins.setLastLoginTime(new Date()); JSONObject resultJSON = this.usersService.saveOneAdminsService(admins); ConstatFinalUtil.LOGGER.info("结果:{}",resultJSON); } }
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd " default-autowire="byName"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.bjsxt"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/userscenter"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- 配置mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置Mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> <!-- 配置Mybatis的映射文件 --> <property name="mapperLocations" value="classpath:com/bjsxt/userscenter/*/pojo/*Mapper.xml"/> </bean> <!-- 为Dao赋值 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 设置BasePackage,到哪些包中照dao接口 --> <property name="basePackage" value="com.bjsxt" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"/> <!-- 配置哪些方法需要开启事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- aop配置 --> <aop:config> <!-- 切入点配置:定义哪些类,开启事务 ,拦截的是接口,注意,不再是实现类 --> <aop:pointcut expression="execution(* com.bjsxt.userscenter.*.service.*.*(..))" id="pc"/> <!-- 织入 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/> </aop:config> </beans>
package com.bjsxt.userscenter.users.service.impl; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONObject; import com.bjsxt.userscenter.common.util.ConstatFinalUtil; import com.bjsxt.userscenter.common.util.PageInfoUtil; import com.bjsxt.userscenter.users.dao.IAdminsDao; import com.bjsxt.userscenter.users.pojo.Admins; import com.bjsxt.userscenter.users.service.IUsersService; /** * 管理员的service的实现类 * @author * */ @Service("usersService") public class UsersServiceImpl implements IUsersService { @Resource private IAdminsDao adminsDao; /** * 保存一条管理员 * @return */ @Override public JSONObject saveOneAdminsService(Admins admins) { JSONObject resultJSON = new JSONObject(); int res = this.adminsDao.save(admins); if (res >0) { //添加成功 JSONObject dataJSON = new JSONObject(); dataJSON.put("id", admins.getId()); //影响条数 dataJSON.put("affect", res +""); resultJSON.put("data", dataJSON); resultJSON.put("code", "0"); resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0")); }else { resultJSON.put("code", "1"); resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1")); } return resultJSON; } @Override public JSONObject updateOneAdminsService(Admins admins) {JSONObject resultJSON = new JSONObject(); int res = this.adminsDao.update(admins); if (res >0) { //添加成功 JSONObject dataJSON = new JSONObject(); dataJSON.put("id", admins.getId()); //影响条数 dataJSON.put("affect", res +""); resultJSON.put("data", dataJSON); resultJSON.put("code", "0"); resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0")); }else { resultJSON.put("code", "2"); resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2")); } return resultJSON; } @Override public JSONObject deleteOneAdminsService(Admins admins) { JSONObject resultJSON = new JSONObject(); int res = this.adminsDao.delete(admins); if (res >0) { //添加成功 JSONObject dataJSON = new JSONObject(); dataJSON.put("id", admins.getId()); //影响条数 dataJSON.put("affect", res +""); resultJSON.put("data", dataJSON); resultJSON.put("code", "0"); resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0")); }else { resultJSON.put("code", "3"); resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3")); } return resultJSON; } @Override public Admins findOneAdminsService(Map<String, Object> condMap) { return this.adminsDao.findOne(condMap); } @Override public List<Admins> findAdminsCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) { //为关键字实现模糊查询 if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword")+"")) { condMap.put("keyword", "%"+condMap.get("keyword")+"%"); } if (pageInfoUtil != null) { //分页 先查询总记录数 condMap.put("pageMarking", "true"); List<Admins> adminsList = this.adminsDao.findCondList(condMap); //取出总记录数 if (adminsList.size() == 1) { pageInfoUtil.setTotalRecord(adminsList.get(0).getId()); } //开始查询数据 condMap.put("pageMarking", "false"); //limit ? ,? condMap.put("currentRecord", pageInfoUtil.getCurrentRecord()); condMap.put("pageSize", pageInfoUtil.getPageSize()); return this.adminsDao.findCondList(condMap); }else { //不进行分页,直接查询 return this.adminsDao.findCondList(condMap); } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你没有给admins起别名,mybatis不认识它。
应该在mybatis的xml配置文件中加上
代码没贴全,看看编译后的文件里有没有这个pojo
mapper里admins没有做aliaes,不能直接引用,还是写类的其全部路径吧,com.xxx.xxx.Admins