mybatis个人小案例(不喜勿喷)

--------------------------------映射文件配置-------------------------------------

<?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">
<!-- 建议namespace为实体类的全路径 后面就可以 实体类.class.getName() -->
<mapper namespace="cn.gdpe.domain.Students">
 <!--resultMap标签:映射实体与表
  type:实体类的全名(包名+类名)
  id:为实体与表的映射娶一个唯一的编号
  -->
 <resultMap type="student" id="student">
  <!-- id标签映射的是主键   property表示的是实体类的属性     column表示的是表的字段
   result映射的其他属性
   -->
  <id property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="sal" column="sal"/>
 </resultMap>
 <!-- id唯一性   
   parameterType要执行的dao中的方法的参数,如果是类的话,必须用全路径(包名+类名)
   
   -->
 <insert id="add" parameterType="student">
  insert into students(id,name,sal) values(#{id},#{name},#{sal})
 </insert>
 <select id="select" parameterType="int" resultType="student">
  select id,name,sal from students where id=#{id}
 </select>
 <select id="selectAll" resultType="student">
  select id,name,sal from students
 </select>
 <update id="update" parameterType="cn.gdpe.domain.Students">
  update students set name=#{name},sal =#{sal} where id=#{id}
 </update>
 <delete id="delete" parameterType="student">
  delete from students where id=#{id}
 </delete>
 
</mapper>

-----------------------------------mybatis配置文件-------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <!-- 资源文件 -->
 <properties resource="db.properties"/>
 <!--此实体类的映射文件中的所有cn.gdpe.dao.StudentDao都可以换成student  -->
 <typeAliases>
  <typeAlias type="cn.gdpe.domain.Students" alias="student"/>
 </typeAliases>
 <environments default="mysql_development">
  <environment id="mysql_development">
   <!--mybatis的事务管理方式  -->
   <transactionManager type="JDBC"/>
   <dataSource type="POOLED">
    <property name="driver" value="${mysql.driver}"/> 
    <property name="url" value="${mysql.url}"/> 
    <property name="username" value="${mysql.username}"/> 
    <property name="password" value="${mysql.password}"/> 
   </dataSource>
  </environment>
  
  <environment id="oracle_development">
   <transactionManager type="JDBC"/>
   <dataSource type="POOLED">
    <property name="driver" value="${oracle.driver}"/> 
    <property name="url" value="${oracle.url}"/> 
    <property name="username" value="${oracle.username}"/> 
    <property name="password" value="${oracle.password}"/> 
   </dataSource>
  </environment> 
 </environments>
 <!-- 映射文件 -->
 <mappers>
  <mapper resource="cn/gdpe/dao/StudentsMapper.xml"/>
 </mappers>
</configuration>

---------------------------------数据源---------------------------------

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://127.0.0.1:3306/mybatis
mysql.username=root
mysql.password=root

oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@127.0.0.1 :1521:orcl
oracle.username=scott
oracle.password=root

---------------------------------工具类----------------------------------------

package cn.gdpe.util;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtil {
 private static ThreadLocal<SqlSession> threadLocal=new ThreadLocal<SqlSession>();
 private static SqlSessionFactory sqlSessoinFactory;
 private MybatisUtil(){}
 static{
  InputStream is=MybatisUtil.class.getResourceAsStream("/mybatis.xml");
  sqlSessoinFactory=new SqlSessionFactoryBuilder().build(is);
 }
 
 public static SqlSession getSqlSession(){
  //从当前线程取出sqlsession
  SqlSession sqlSession=threadLocal.get();
  //如果为空
  if(sqlSession==null){
   //就从sqlSessoinFactory中打开一个sqlSession
   sqlSession=sqlSessoinFactory.openSession();
   //将session与当前线程绑在一起
   threadLocal.set(sqlSession);
  }
  return sqlSession;
 }
 
 public static void closeSqlSession(){
  //从当前线程取出sqlsession
  SqlSession sqlSession=threadLocal.get();
  //如果不为空
  if(sqlSession!=null){
   //将session与当前线程分开
   sqlSession.close();
   threadLocal.remove();
  }
 }
 public static void main(String[] args){
  getSqlSession().getConnection();
  System.out.println("连接成功");
  closeSqlSession();
 }
}

------------------------------------dao测试类--------------------------------------------

package cn.gdpe.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.gdpe.domain.Students;
import cn.gdpe.util.MybatisUtil;

public class StudentDao {
 @Test
 public void add(){
  SqlSession sqlSession=null;
  try{
   Students s=new Students();
   s.setId(5);
   s.setName("ly4");
   s.setSal(10000.0);
   sqlSession= MybatisUtil.getSqlSession();
   sqlSession.insert(Students.class.getName()+".add",s);
   //mybatis必须手动提交事务,否则不会向数据库插入数据
   sqlSession.commit();
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }
// @Test
 public Students select(){
  SqlSession sqlSession=null;
  try{
   sqlSession= MybatisUtil.getSqlSession();
   Students student =sqlSession.selectOne(Students.class.getName()+".select", 5);
   //mybatis必须手动提交事务,否则不会向数据库插入数据
   sqlSession.commit();
   return student;
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
   return null;
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }
// @Test
 public void selectAll(){
  SqlSession sqlSession=null;
  try{
   sqlSession= MybatisUtil.getSqlSession();
   List<Students> students =sqlSession.selectList(Students.class.getName()+".selectAll");
   //mybatis必须手动提交事务,否则不会向数据库插入数据
   for(Students student : students){
    System.out.println(student);
   }
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
  }finally{
  }
 }
 @Test
 public void update(Students student){
  SqlSession sqlSession=null;
  try{
   sqlSession= MybatisUtil.getSqlSession();
   //mybatis必须手动提交事务,否则不会向数据库插入数据
   sqlSession.update(Students.class.getName()+".update",student);
   sqlSession.commit();
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }
 @Test
 public void delete(){
  SqlSession sqlSession=null;
  try{
   sqlSession= MybatisUtil.getSqlSession();
   Students student =sqlSession.selectOne(Students.class.getName()+".select", 3);
   sqlSession.delete(Students.class.getName()+".delete", student);
   sqlSession.commit();
  }catch(Exception e){
   e.printStackTrace();
   sqlSession.rollback();
  }finally{
   MybatisUtil.closeSqlSession();
  }
 }
 public static void main(String[] args){
  StudentDao dao=new StudentDao();
  Students student=dao.select();
  student.setName("你吗");
//  student.setSal(20000D);
  dao.update(student);
 }
 
 
}

---------------------------细节问题----------------------------------

1:当实体类属性与数据库表字段名称不一致时:

<resultMap type="student" id="studentMap">
  <!-- id标签映射的是主键   property表示的是实体类的属性     column表示的是表的字段
   result映射的其他属性
   -->
  <id property="id" column="s_id"/>
  <result property="name" column="s_name"/>
  <result property="sal" column="s_sal"/>
 </resultMap>

 

<select id="select" parameterType="int" resultMap="studentMap">
          select s_id,s_name,s_sal from students where s_id=#{id}
 </select>

转载于:https://my.oschina.net/chenliyong/blog/672498

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值