发表于: 2017-10-18 16:59:31
1 694
修改时间类型
今日完成17-21
1.Mybatis的xml文件编写
理解没标签的用途
<!-- 配置 mybatis.xml -->
<!-- 读取配置文件 -->
<util:properties id="jdbc"
location="classpath:conf/jdbc.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="#{jdbc.driver}"/>
<property name="url"
value="#{jdbc.url}"/>
<property name="username"
value="#{jdbc.user}"/>
<property name="password"
value="#{jdbc.password}"/>
</bean>
<!-- 配置MyBatis的 SessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="mapperLocations"
value="classpath:mapper/*.xml"/>
</bean>
<!-- Mapper接口组件扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.yunciiot.xiuzhen.dao"/>
</bean>
2.学生类这里需要
很重要:这里需要实现Serializable 序列化
long类型要改成长整型
原因...百度文档
package com.yunciiot.xiuzhen.domain;
import java.io.Serializable;
/**
* 学生类
* @author Administrator
*
*/
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 学生id
*/
private String id;
/**
* 学生姓名
*/
private String name;
/**
* 学生qq号
*/
private String qq;
/**
* 修真类型
*/
private String type;
/**
* 学生毕业学校
*/
private String school;
/**
* 学生立愿
*/
private String will;
/**
* 创建时间
*/
private Long createat;
/**
* 修改时间
*/
private Long updateat;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getWill() {
return will;
}
public void setWill(String will) {
this.will = will;
}
public Long getCreateat() {
return createat;
}
public void setCreateat(Long createat) {
this.createat = createat;
}
public Long getUpdateat() {
return updateat;
}
public void setUpdateat(Long updateat) {
this.updateat = updateat;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", qq=" + qq + ", type=" + type + ", school=" + school
+ ", will=" + will + ", createat=" + createat + ", updateat=" + updateat + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
3.DAO层
通过映射去查找mapper的XMl文件的SQL语句
package com.yunciiot.xiuzhen.dao;
import com.yunciiot.xiuzhen.domain.Student;
public interface StudentDao {
/**
* 通过id查找学生
* @param id
* @return
*/
Student findStudnetById(String id);
/**
* 添加学生
* @param student
* @return
*/
int insertStudent(Student student);
/**
* 通过学生id修改学生
* @param student
* @return
*/
int updateStudnetById(Student student);
/**
* 通过学生的id删除学生
* @param student
* @return
*/
int deleteSutdnet(Student student);
}
4.Mapper的XML文件编写
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.yunciiot.xiuzhen.dao.StudentDao">
<!-- 查询学生 -->
<select id="findStudnetById" parameterType="string"
resultType="com.yunciiot.xiuzhen.domain.Student">
SELECT
stu_id as id,
stu_name AS name,
stu_qq AS qq,
stu_type AS type,
stu_school AS school,
stu_will AS will,
stu_create_at AS createat,
stu_update_at AS updateat
FROM student
WHERE stu_id=#{id}
</select>
<!-- 添加学社 -->
<insert id="insertStudent" parameterType="com.yunciiot.xiuzhen.domain.Student">
INSERT INTO student(
stu_name,
stu_qq,
stu_type,
stu_school,
stu_will,
stu_create_at,
stu_update_at
)VALUES(
#{name},#{qq},#{type},#{school},#{will},UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW())
)
</insert>
<!-- 修改学生信息 -->
<update id="updateStudnetById"
parameterType="com.yunciiot.xiuzhen.domain.Student">
UPDATE student
SET
<if test="name != null">
stu_name=#{name},
</if>
<if test="qq != null">
stu_qq=#{qq},
</if>
<if test="type != null">
stu_type=#{type},
</if>
<if test="school != null">
stu_school=#{school},
</if>
<if test="will != null">
stu_will=#{will},
</if>
stu_update_at=UNIX_TIMESTAMP(NOW())
WHERE stu_id=#{id}
</update>
<!--删除学生信息 -->
<delete id="deleteSutdnet" parameterType="com.yunciiot.xiuzhen.domain.Student">
DELETE FROM STUDENT WHERE stu_id=#{id}
</delete>
</mapper>
5.junti测试
单元测试可以测试已写好的一部分功能,可以更直观的了解了解代码是否有问题,
较少日后的问题
5.待解决问题log4j只要打印sql
评论