基于mybatis的增删改查
来源:互联网 发布:最优化方法孙文瑜辅导 编辑:程序博客网 时间:2024/06/05 03:14
1.db.properties
mysql.driver=com.mysql.jdbc.Drivermysql.url=jdbc:mysql://127.0.0.1:3306/mybatismysql.username=rootmysql.password=123456oracle.driver=oracle.jdbc.driver.OracleDriveroracle.url=jdbc:oracle:thin:@127.0.0.1:1521:mldnoracle.username=scottoracle.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
- 基于mybatis的增删改查
- Mybatis-基于注解的增删改查
- 基于Mybatis的增删改查
- mybatis的增删查改
- mybatis的增删查改
- Mybatis的增删改查
- mybatis的 增删改查
- mybatis的增删改查
- 基于Spring,Struts2,mybatis的增删改查
- MyBatis(三)增删改查--基于接口
- MyBatis(四)增删改查--基于注解
- Mybatis 增删改查
- mybatis增删改查
- mybatis增删改查
- mybatis 增删改查
- MyBatis增删改查
- mybatis--增删改查
- MyBatis增删改查
- 写给VR手游开发小白的教程:(四)补充篇,详细介绍Unity中相机的投影矩阵
- 面向对象_成员方法的分类及使用
- C#读取并保存导出Resource资源文件
- 未名
- 1008. Elevator (20)
- 基于mybatis的增删改查
- Android蓝牙源码分析——Gatt连接(一)
- UVA - 10856 Recover Factorial(二分查找)
- 经典算法学习——第一个只出现一次的字符
- CSS Hack
- 安装IIS6,不能复制文件的解决办法
- 心得之gets
- 题目
- Merit价值和成果管理系统——1侧栏与iframe