首页 > 其他分享 >mybatis dao 和 映射xml

mybatis dao 和 映射xml

时间:2023-05-29 12:33:52浏览次数:73  
标签:xml name dao clazz public student mybatis import id


<!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

相关文章

  • 微信小程序 - WXML 模板语法
    【黑马程序员前端微信小程序开发教程,微信小程序从基础到发布全流程_企业级商城实战(含uni-app项目多端部署)】https://www.bilibili.com/video/BV1834y1676P/?p=19&share_source=copy_web&vd_source=03c1dc52eeb3747825ecad0412c18ab1真的讲的很细致WXML模板语法数据绑定1.......
  • MyBatis源码之getMapper获取代理对象
    MyBatis源码之getMapper获取代理对象下面这句话意思非常明了,就是通过传入接口类型对象,获取接口代理对象。IUserDaouserDao1=sqlSession1.getMapper(IUserDao.class);具体的过程如下:一、首先,调用SqlSession的实现类DefaultSqlSession的getMapper方法,其实是在该方法内调用con......
  • Mybatis-plus做分页和联表查询
    示例首先先从代码上看一下我在框架中是如何使用的分页:注: AboutusHomepage是实体类 1.Controller@RequestMappingpublicStringindex(@RequestParam(value="pageNo",required=false,defaultValue="1")IntegerpageNo,@Reque......
  • mybatis 动态查询
    可以不用where标签然后拼一个1=1   when至少有一个分支会执行    批量删除   批量插入   多表联查   里面有集合的时候oftype  分页    ......
  • mybatis 查询 map 多参数
         返回map 返回一个大map 主键id作为keyvalue本身作为map  ......
  • Java学习:使用MyBatis Plus的分页插件和QueryWrapper结合自定义mapper xml实现多表关联
     Vo:/***用来返回给前端展示列表的数据实体*/@DatapublicclassCourseVoimplementsSerializable{privatestaticfinallongserialVersionUID=1L;privateStringid;privateStringtitle;privateStringsubjectParentTitle;private......
  • springboot整合mybatis实现简单的crud操作
    使用MyBatis框架操作数据,在SpringBoot框架集成MyBatis,项目整体结构前提:准备一张student表。SETNAMESutf8mb4;SETFOREIGN_KEY_CHECKS=0;--------------------------------Tablestructureforuser------------------------------DROPTABLEIFEXISTS`student`......
  • Mybatis-plus中自定义的sql语句调用QueryWrapper实现查询
     一、引言MP自带的条件构造器虽然很强大,有时候也避免不了写稍微复杂一点业务的sql,那么今天说说MP怎么自定义sql语句吧。 二、具体实现使用注解实现:在我们Mapper接口中定义自定义方法即可。/***@Date:2019/6/1014:40*@Description</span>:User对象持久层*/p......
  • mybatis plus 校验用户名是否重复
    添加//判断登录名的唯一性QueryWrapper<User>query=newQueryWrapper<>();query.lambda().eq(User::getLoginName,user.getLoginName());Userone=userService.getOne(query);if(one!=null&&one.getUserId()!=user.getUserId()){returnResult......
  • Java:mybatis-plus-generator-ui 基于Mybatis-Plus的代码自助生成器
    引用官方文档上的简介:提供交互式的WebUI用于生成兼容mybatis-plus框架的相关功能代码,包括Entity,Mapper,Mapper.xml,Service,Controller等,可以自定义模板以及各类输出参数,也可通过SQL查询语句直接生成代码。文档github:https://github.com/davidfantasy/mybatis-plus-generator-......