<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jaeson.mybatis.dao.StudentMapper">
<resultMap type="com.jaeson.mybatis.model.Student" id="studentmap">
<id column="student_id" property="id"/>
<result column="student_name" property="name"/>
<result column="student_sex" property="sex"/>
<association property="clazz" javaType="com.jaeson.mybatis.model.Clazz">
<id property="id" column="clazz_id"/>
<result property="name" column="clazz_name"/>
</association>
</resultMap>
<sql id="Base_Column_List" >
s.id as student_id,
s.name as student_name,
s.sex as student_sex,
s.clazz_id as student_clazz,
c.id as clazz_id,
c.name as clazz_name
</sql>
<select id="getStudent" parameterType="java.lang.String" resultMap="studentmap">
select
<include refid="Base_Column_List" />
from student s
left outer join clazz c on s.clazz_id=c.id
where s.id=#{id}
</select>
<select id="getAll" resultMap="studentmap">
select
<include refid="Base_Column_List" />
from student s
left outer join clazz c on s.clazz_id=c.id
</select>
<insert id="save" parameterType="com.jaeson.mybatis.model.Student" >
insert into student
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="1==1" >
id,
</if>
<if test="1==1" >
name,
</if>
<if test="1==1" >
sex,
</if>
<if test="clazz!=null" >
clazz_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="1==1" >
#{id},
</if>
<if test="1==1" >
#{name},
</if>
<if test="1==1" >
#{sex},
</if>
<if test="clazz!=null" >
#{clazz.id},
</if>
</trim>
</insert>
<update id="update" parameterType="com.jaeson.mybatis.model.Student">
update student set
name=#{name},
sex=#{sex}
<if test="clazz!=null" >
,clazz_id=#{clazz.id}
</if>
where id=#{id}
</update>
<delete id="delete" parameterType="java.lang.String">
delete from student where id=#{id}
</delete>
</mapper>
package com.jaeson.mybatis.dao;
import java.util.List;
import com.jaeson.mybatis.model.Student;
public interface StudentMapper {
public int save(Student student);
public int update(Student student);
public int delete(String id);
public Student getStudent(String id);
public List<Student> getAll();
}
package com.jaeson.mybatis.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jaeson.mybatis.model.Student;
import com.jaeson.mybatis.service.StudentService;
import com.jaeson.mybatis.dao.StudentMapper;
@Service
public class StudentServiceImpl implements StudentService {
private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);
@Autowired
private StudentMapper mapper;
@Override
public int save(Student student) {
logger.info("save student(id={}, name={})", student.getId(), student.getName());
return mapper.save(student);
}
@Override
public int update(Student student) {
logger.info("update student(id={}, name={})", student.getId(), student.getName());
return mapper.update(student);
}
@Override
public int delete(String id) {
logger.info("delete student(id={})", id);
return mapper.delete(id);
}
@Override
public Student getStudent(String id) {
logger.info("get student(id={})", id);
return mapper.getStudent(id);
}
@Override
public List<Student> getAll() {
logger.info("getAll()");
return mapper.getAll();
}
}
标签:xml,name,dao,clazz,public,student,mybatis,import,id From: https://blog.51cto.com/u_16131764/6370076