这篇文章主要介绍“Mybatis的sql注释问题怎么解决”,在日常操作中,相信很多人在Mybatis的sql注释问题怎么解决问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Mybatis的sql注释问题怎么解决”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Mybatis的sql注释
//mapper下的sql注释package com.msb.mapper;import com.msb.pojo.Dept;import com.msb.pojo.Emp;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.awt.*;public interface EmpTestMapper { @Select("select * from dept where deptno =#{deptno}") Dept findByDeptno(int deptno); @Update("update dept set dname =#{dname}, loc =#{loc} where deptno =#{deptno}") int updateDept(Dept dept); @Insert("insert into dept values(DEFAULT,#{dname},#{loc})") int addDept(Dept dept); @Delete("delete from dept where deptno =#{deptno}") int removeDept(int deptno);}
<?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.msb.mapper.EmpTestMapper"> <!--resultMap是一个映射表,对应的字段会对应数据表的字段--> <!-- List<Emp> findEmp();--></mapper>
import com.msb.mapper.DeptMapper;import com.msb.mapper.EmpMapper;import com.msb.mapper.EmpTestMapper;import com.msb.pojo.Dept;import com.msb.pojo.Emp;import com.msb.pojo.Projectrecord;import com.msb.pojo.Projects;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.List;public class Test1 { SqlSession sqlSession = null; EmpMapper mapper =null; @Before public void test1(){ //首先做一个对象SqlSessionFactoryBuilder建立一个绘话 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); //有一个文本输入的io流进行读取操作 InputStream stream = null; try { //这里的路径直接会定位到配置文件classes下面;所以这个文件在次目录下--编译和 //-图纸;对数据库文件进行读取,获取一个io流,由于配置文件在classes下面,直接写文件名即可 stream = Resources.getResourceAsStream("sqlMapConfig.xml"); } catch (IOException e) { e.printStackTrace(); } //build需要指向一个文件进行读取出来--工厂 SqlSessionFactory factory = ssfb.build(stream); //需要用sqlSession去调用增删改查--工人去获取数据,打开这个绘话 sqlSession = factory.openSession(true); } @Test public void test4(){ EmpTestMapper mapper = sqlSession.getMapper(EmpTestMapper.class); Dept emp = mapper.findByDeptno(20); Dept dept = new Dept(); dept.setDeptno(null); dept.setLoc("大格"); dept.setDname("水军"); int addDept = mapper.addDept(dept); System.out.println(addDept); System.out.println(emp); } @After public void test3(){ if (sqlSession!=null){ sqlSession.close(); } }}
到此,关于“Mybatis的sql注释问题怎么解决”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!