基于mybatis的增删改查

1.db.properties

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

oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:mldn
oracle.username=scott
oracle.password=tiger

2.StudentMapper.xml

<?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="cn.itcast.app02.Student">

    <resultMap type="cn.itcast.app02.Student" id="studentMap">
       <id property="id" column="id"></id>
       <result property="name" column="name"/>
       <result property="sal" column="sal"/> 
    </resultMap>
     <!-- 增加数据-->
    <insert id="add" parameterType="cn.itcast.app02.Student">
        insert into students(id,name,sal) values(#{id},#{name},#{sal});
    </insert>
    
    <!-- 根据id查询 -->
    <select id="findById" parameterType="int" resultType="cn.itcast.app02.Student">
        select id,name,sal from students where id=#{id};
    </select>
    
     <!-- 查询全部数据-->
    <select id="findAll" resultType="cn.itcast.app02.Student">
        select id,name,sal from students;
    </select>
    
     <!-- 更新数据-->
     <update id="update" parameterType="cn.itcast.app02.Student">
         update students set name=#{name},sal=#{sal} where id=#{id};
     </update>
     
     <!-- 更新数据-->
     <delete id="delete" parameterType="cn.itcast.app02.Student">
         delete from students where id=#{id};
     </delete>

</mapper>

3.StudenDao

package cn.itcast.app02;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.itcast.util.MybatisUtil;

public class StudentDao {
	//增加数据
	public void add(Student student) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			sqlSession.insert(Student.class.getName()+".add",student);
			sqlSession.commit();
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
				
	}
	//根据id查询数据
	public Student findById(int id) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			Student student=sqlSession.selectOne(Student.class.getName()+".findById",id);
			sqlSession.commit();
			return student;
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
				
	}
	
	//根据id查询数据
	public List<Student> findAll() throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			List<Student> studentList=sqlSession.selectList(Student.class.getName()+".findAll");
			sqlSession.commit();
			return studentList;
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
					
	}
	
	//根据id查询数据
	public void update(Student student) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			int i=sqlSession.update(Student.class.getName()+".update",student);
			sqlSession.commit();
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
				
	}
	//根据id查询数据
	public void delete(Student student) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			sqlSession.delete(Student.class.getName()+".delete",student);
			sqlSession.commit();
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
				
	}
	
	
	//测试
	public static void main(String[] args) throws Exception{
		StudentDao dao=new StudentDao();
		//dao.add(new Student(1,"能源1",1000D));
		//dao.add(new Student(2,"能源2",2000D));
		//dao.add(new Student(3,"能源3",3000D));
		//dao.add(new Student(4,"能源4",4000D));
		//dao.add(new Student(5,"能源5",5000D));
		//Student student=dao.findById(4);
		/*List<Student> studentList=dao.findAll();
		for(Student student:studentList){
		   System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getSal());
		}*/
		Student student=dao.findById(5);
		
		dao.delete(student);
	}

}

4.mybatis.xml

<?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"></properties>
  
  <typeAliases>
      <typeAlias type="cn.itcast.app01.Student" alias="Student"></typeAlias>
  </typeAliases>

  <environments default="mysql_developer">
     <environment id="mysql_developer">
        <transactionManager type="jdbc"></transactionManager>
        <dataSource type="pooled">
          <property name="driver" value="${mysql.driver}"></property>
          <property name="url" value="${mysql.url}"></property>
          <property name="username" value="${mysql.username}"></property>
          <property name="password" value="${mysql.password}"></property>  
        </dataSource>
     </environment>
     
     <environment id="oracle_developer">
        <transactionManager type="jdbc"></transactionManager>
        <dataSource type="pooled">
          <property name="driver" value="${oracle.driver}"></property>
          <property name="url" value="${oracle.url}"></property>
          <property name="username" value="${oracle.username}"></property>
          <property name="password" value="${oracle.password}"></property>  
        </dataSource>
     </environment>
  </environments>
  
  <mappers>
     <mapper resource="cn/itcast/app02/StudentMapper.xml"></mapper>
  </mappers>
</configuration>

5.Student

package cn.itcast.app02;

public class Student {
	private Integer id;
	private String name;
	private Double sal;
	
	public Student(){
		
	}

	public Student(Integer id, String name, Double sal) {
		super();
		this.id = id;
		this.name = name;
		this.sal = sal;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getSal() {
		return sal;
	}

	public void setSal(Double sal) {
		this.sal = sal;
	}
	
	

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值