12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
mysql数据库 时间:2024-12-25 09:57:32
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
import java.util.*;import cn.mybatis.entity.Student;import cn.mybatis.util.MybatisUtil;import org.apache.ibatis.session.SqlSession;public class StudentDao {/**
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
import java.util.*;import cn.mybatis.entity.Student;import cn.mybatis.util.MybatisUtil;import org.apache.ibatis.session.SqlSession;public class StudentDao {/** * 增加学生 */public void add(Student student) throws Exception{SqlSession sqlSession = null;try{sqlSession = MybatisUtil.getSqlSession();//事务开始(默认)//读取StudentMapper.xml映射文件中的SQL语句int i = sqlSession.insert(Student.class.getName()+".add",student);System.out.println("本次操作影响了"+i+"行");//事务提交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();}}/** * 查询所有学生 */public List<Student> findAll() throws Exception{SqlSession sqlSession = null;try{sqlSession = MybatisUtil.getSqlSession();return sqlSession.selectList(Student.class.getName()+".findAll");}catch(Exception e){e.printStackTrace();throw e;}finally{MybatisUtil.closeSqlSession();}}/** * 更新学生 */public void update(Student student) throws Exception{SqlSession sqlSession = null;try{sqlSession = MybatisUtil.getSqlSession();sqlSession.update(Student.class.getName()+".update",student);sqlSession.commit();}catch(Exception e){e.printStackTrace();sqlSession.rollback();throw e;}finally{MybatisUtil.closeSqlSession();}}/** * 删除学生 */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(3,"美丽",70030.3));//dao.add(new Student(4,"加油",70030.3));//dao.add(new Student(5,"关系",70030.3));//dao.add(new Student(6,"规律",70030.3));//dao.add(new Student(7,"古蔺",70030.3));// List<Student> studentslist = dao.findAll();//for (Student student : studentslist ) {//System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());//}//Student student = dao.findById(4);//student.setName("liwen");//dao.update(student);}}
<?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.mybatis.entity.Student"><resultMap type="cn.mybatis.entity.Student" id="studentMap"><id property="id" column="id"/><result property="name" column="name"/><result property="sal" column="sal"/></resultMap><!-- 增加学生 --><insert id="add" parameterType="cn.mybatis.entity.Student">insert into students(id,name,sal) values(#{id},#{name},#{sal})</insert><!-- 根据ID查询学生 如果参数不是一个实体的话,只是一个普通变量,例如:int,double,String 这里的#{中间的变量名可以随便写},不过提倡就用方法的形参 --><select id="findById" parameterType="int" resultType="cn.mybatis.entity.Student">select id,name,sal from students where id = #{id}</select><!-- 查询所有学生 理论上resultType要写List<Student> 但这里只需书写List中的类型即可,即只需书写Student的全路径名--><select id="findAll" resultType="cn.mybatis.entity.Student">select id,name,sal from students</select><!-- 更新学生 --><update id="update" parameterType="cn.mybatis.entity.Student">update students set name=#{name},sal=#{sal} where id=#{id}</update><!-- 删除学生 --><delete id="delete" parameterType="cn.mybatis.entity.Student">delete from students where id = #{id}</delete><!-- 无条件分页 --><select id="findAllWithFy" parameterType="map" resultMap="studentMap">select id,name,sal from students limit #{pstart},#{psize}</select>
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19