发表于: 2018-03-21 23:53:16
3 606
一。插入数据的时候,id是004,结果报错:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '004' for key 'PRIMARY'
MySQLIntegrityConstraintViolationException:键约束校验异常,004这个字段的数据违反了主键的约束,也许是外键约束呀或者是唯一键等等的。
后面看了下实体类,采取了唯一键,然后数据库中id字段中已经存在了004值。
此时是违反了唯一键约束。把它改成005即可。主键本身就自带唯一键约束
二。尝试解读一下applicationContext.XML文件
<?xml version="1.0" encoding="UTF-8"?>
<beans 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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<!-- 自动扫描 -->
<context:component-scan base-package="com.dong.service"/>
<context:component-scan base-package="com.dong.action"/>
<context:component-scan base-package="com.dong.dao"/>
<context:property-placeholder location="classpath:properties/jdbc.properties"/>
<!-- 配置数据源-->
<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbcdriverClassName}"/>
<property name="url" value="${jdbcurl}"/>
<property name="username" value="${jdbcusername}"/>
<property name="password" value="${jdbcpassword}"/>
</bean>
<bean id="StudentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.dong.dao.StudentDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource"/>
<property name="mapperLocations" value="classpath*:/*Mapper.xml"/>
</bean>
</beans>
表头轻易不要动,以后再加入什么,直接alt+enter就可以,自然会引用出来。
<context:component-scan base-package="com.dong.service"/>
<context:component-scan base-package="com.dong.action"/>
<context:component-scan base-package="com.dong.dao"/>
这三个可以用一个<context:component-scan base-package= "com.dong.*"/>代替。
<context:property-placeholder location="classpath:properties/jdbc.properties"/>这句话相当于加载了jdbc
后面就是jdbc的配置,这个不用说了,调用的是jdbc.properties文件里面配置好的文件,作用就是连接数据库
最后面的三句话,是调用mapper.xml。作用是调用SQL语句。SQL语句的具体执行,在mapper里面。
上面的代码,就是整合mapper.xml和dao
<?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.dong.dao.StudentDao">
<insert id="saveStudent" parameterType="com.dong.entity.Student">
insert into t_app_student (id,name,sex,age) values (#{id},#{name},#{sex},#{age})
</insert>
<select id="getStudent" resultType="com.dong.entity.Student" parameterType="java.lang.String">
select * from t_app_student where id =#{id}
</select>
</mapper>
mapper.xml文件里面,第一句SQL语句,就是往数据库里添加数据
第二句就是根据id查询其他数据
saveStudent和getStudent是两个方法,parameterType是参数类型,resultType是是结果类型
mapper就属于mybatis了。
<?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>
<typeAliases>
<typeAlias type="com.dong.entity.Student" alias="Student"/>
</typeAliases>
</configuration>
mybatis-config里面这次没什么用,注释掉也是可以的。问题是,为什么明明是做的spring+mybatis,mybatis配置文件反倒没什么用了呢?只是在mapper里面起了作用。
json文件看了一天,只弄懂了一点点东西,JSP页面中输出json数据,以后做项目的时候,可以给前端提供假数据,然后自己写接口,等完成之后,再给前端一套另外的数据,这样可以提供效率。
明天的计划:任务一这样就复习完了,准备小课堂,有机会还是要学一下spring
遇到的问题:暂无
今天的收获:做了个spring+mybatis
java任务二开始时间:2018.01.25
预计demo时间:2018.02.12
可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论