欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis使用实例

程序员文章站 2022-07-13 15:51:28
...

 

mybatis使用说明

1.配置文件
2.PO 映射对象
3.Dao
4.对象关联:使用外连接查询
5.分页:sql分页 limit 10
6.mybatis 调用存储过程
7.sqlSession close异常?

public class TestDaoImpl implements ITestDao {
 private SqlSessionTemplate sqlSessionTemplate;

 @Override
 public List<TestPO> query(TestPO po) {
  List<TestPO> list = sqlSessionTemplate.selectList("testMapper.query", po);
  return list;
 }

 @Override
 public boolean add(TestPO po) {
  int i = sqlSessionTemplate.insert("testMapper.add", po);
  boolean suc = false;
  if(i==1){
   suc = true;
  }
  return suc;
 }

 @Override
 public boolean update(TestPO po) {
  int i = sqlSessionTemplate.update("testMapper.update", po);
  boolean suc = false;
  if(i==1){
   suc = true;
  }
  return suc;
 }

 @Override
 public boolean delete(String id) {
  int i = sqlSessionTemplate.delete("testMapper.delete", id);
  boolean suc = false;
  if(i==1){
   suc = true;
  }
  return suc;
 }

 @Override
 public void proc(Map<String, Object> map) {
  sqlSessionTemplate.select("testMapper.procedure", map, null);
 }

 public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
 
  this.sqlSessionTemplate = sqlSessionTemplate;
 }
}

<?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="testMapper">
 <!-- po对象属性映射 -->
 <resultMap id="testMap" type="cn.demo.model.po.TestPO" >
  <result property="userId" column="userId"/>
  <result property="userName" column="userName"/>
  <result property="birthday" column="birthday"/>
  <result property="salary" column="salary"/>
  <result property="ip" column="ip"/>
  <result property="id" column="id"/>
  <result property="content" column="content"/>
  <result property="image" column="image"/>
 </resultMap>
 
 <!-- sql 片段 -->
 <sql id="userFields">
  userId,userName,birthday,salary
 </sql>
 
 <!-- 多表连接查询 -->
 <select id="query" parameterType="java.util.Map" resultMap="testMap">
  select <include refid="userFields"/>,
  B.ip,B.id
  from user A,log B
  where A.userId = B.id
  <if test="userName != '' and userName != null">
   AND A.userName = #{userName}
  </if>
 </select>
 
 <insert id="add" parameterType="cn.demo.model.po.TestPO">
  insert into user(<include refid="userFields"/>)
  values(#{userId, jdbcType=VARCHAR},
  #{userName, jdbcType=VARCHAR},
  #{birthday, jdbcType=DATE},
  #{salary, jdbcType=NUMERIC}
  )
 </insert>
 
 <update id="update" parameterType="cn.demo.model.po.TestPO">
  update user set userName=#{userName},
  birthday = now()
  where userId = #{userId}
 </update>
 
 <!-- CDATA括号内的内容不会被解析,当成文本看待 -->
 <delete id="delete" parameterType="string">
  <![CDATA[ delete from user where userId = #{userId} ]]>
 </delete>

 <!-- mybatis调用存储过程: statementType-CALLABLE -->
 <select id="procedure" statementType="CALLABLE">
  {call p_sum_salary(#{dte, jdbcType=DATE, mode=IN}, #{amt, jdbcType=NUMERIC, mode=OUT})}
 </select>
</mapper>

public class TestPO implements Serializable{
 private String userId;
 private String userName;
 private Date birthday;
 private double salary;

 private String ip;
 private int id;
 //clob
 private String content;
 //blob
 private byte[] image;

 public String getUserId() {
  return userId;
 }

 public void setUserId(String userId) {
  this.userId = userId;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public Date getBirthday() {
  return birthday;
 }

 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }

 public double getSalary() {
  return salary;
 }

 public void setSalary(double salary) {
  this.salary = salary;
 }

 public String getIp() {
  return ip;
 }
 public void setIp(String ip) {
  this.ip = ip;
 }
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }
 
 public String getContent() {
  return content;
 }
 
 public void setContent(String content) {
  this.content = content;
 }

 public byte[] getImage() {
  return image;
 }

 public void setImage(byte[] image) {
  this.image = image;
 }
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/*.xml"})
public class DaoTest {
 
 @Autowired
 private ITestDao dao;
 private static Logger log = LoggerFactory.getLogger(DaoTest.class);
 
 @org.junit.Test
 public void query(){
  TestPO po = new TestPO();
  po.setUserName("feifei2");
  List<TestPO> list = dao.query(po);
  log.info("query() list length: "+list.size());
 }
 @org.junit.Test
 public void add(){
  TestPO po = new TestPO();
  po.setUserId("4003");
  po.setUserName("feifei2");
  po.setBirthday(new Date());
  po.setImage("abcd123".getBytes());
  po.setSalary(50);
  boolean f = dao.add(po);
  log.info("add() : "+f);
 }
 @org.junit.Test
 public void update(){
  TestPO po = new TestPO();
  po.setUserId("4000");
  po.setUserName("feifei");
  po.setBirthday(new Date());
  po.setSalary(550);
  boolean f = dao.update(po);
  log.info("update() : "+f);
 }
 @org.junit.Test
 public void delete(){
  String id ="4000";
  boolean f =dao.delete(id);
  log.info("delete() :"+f);
 }
 //调用存储过程
 @org.junit.Test
 public void proc(){
  Map<String,Object> map = new HashMap<String,Object>();
  map.put("dte", new Date());//入参:IN
  dao.proc(map);
  BigDecimal amt = (BigDecimal)map.get("amt"); //出参:OUT
  log.info("proc() amt = "+amt);
 }
}

 delimiter //
 create  procedure p_sum_salary(IN dte Date,OUT amt int)
 begin
 select 100 into amt;
 end
 //

 

相关标签: mybatis