发表于: 2018-01-22 23:44:55
4 647
编辑日报内容...
今天是一个值得庆祝的日子,我终于看着教程,只凭自己,把spring+mybatis搭建好了,大概用了半小时。如果所有代码都自己打的话,估计要2-3个小时。
中间,出现了几次报错,也就是几个点值得注意一下
1.test文件,还是要放在IDEA,test。java。test包下面
2.XML文件,都放在resources里面
报错。"main" org.apache.ibatis.binding.BindingException: Invalid bound statement
我把UserDao.xml里面的
<mapper namespace="comNaNteng.dao.UserDao">
这句话改成了如图这个样子,以前不是这样的。这样,运行,就成功了。
如图。
这只是XML成功了,还要保存,复制成注解方式的,再打开运行成功才好。
二。下面尝试用注解的方式搭建spring-mybatis
UserDao里面
public interface UserDao {
@Insert("INSERT INTO user (id,username,password) VALUES (#{id},#{username},#{password})")
public int add(User user);
@Delete("DELETE FROM user WHERE id=#{id}")
public void delete(int id);
@Select(" SELECT * FROM user WHERE id=#{id} ")
public User get(int id);
@Update("UPDATE user SET password=#{password},username=#{username} WHERE id=#{id}")
public int update(User user);
@Select("select * from user")
public List<User> list();
public int count();
,然后user.xml可以删除sql语句,变成:
?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.ptteng.dao.UserDao">
</mapper>
mybatis-config.xml里面
<?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>
<mappers>
<mapper resource="UserDao.xml"/>
<mapper class="com.ptteng.model.User"/>
</mappers>
</configuration>
ApplicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/springmybaitis</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1234</value>
</property>
</bean>
<bean id="SqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.ptteng.model" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:User.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ptteng.dao"/>
</bean>
</beans>
运行报错:org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [mybatis-config.xml]';
把ApplicationContext.xml文件里面的
<property name="mapperLocations" value="classpath:mybatis-config.xml"/>
红色字体改成User.xml
Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [UserDao.xml]'
解决方案:还是ApplicationContext.xml里面 SqlSession部分:
<property name="typeAliasesPackage" value="comNaNteng.model" />
这里应该写类的路径,而不是接口的路径,我以前写的是comNaNteng.dao
Invalid bound statement (not found): comNaNteng.dao.UserDao.delete
UserDao.xml里面(后来被我改成User.xml了)
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id=#{id}
</delete>
把int改成User
跑通了。
然后把SpringMybatisAnnotation和SpringMybatisXml都上传到SVN。
明天的计划:准备小课堂,如果可以,上传SVN
遇到的问题:暂无
今天的收获:自己比照着教程做了注解和XML两种,都跑通了
java任务一开始时间:2017.12.05
预计demo时间:2018.01-05
可能有延期风险,原因是:已经延期了,基础比较差,
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论