MyBatis的两个主要配置文件

mytatis.xml:放在src目录下,常见的配置如下

<?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>
  <!--1-->
<properties resource="db.properties"/>
  <!--2-->
<typeAliases>
<typeAlias type="com.winner.entity.Student" alias="Student"/>
</typeAliases>
  <!--3-->
<environments default="mysql_developer">
<environment id="mysql_developer">
<transactionManager type="JDBC"></transactionManager>
<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_developer">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</dataSource>
</environment>
</environments>
  <!--4-->
<mappers>
<mapper resource="com/winner/entity/StudentMapper.xml"/>
</mappers>
</configuration>

 SQL语句后面不要有;

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"> <!--namespace写成类的全限定名有好处,在Dao中方便-->
<mapper namespace="com.winner.entity.Student"> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<resultMap id="studentMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap> <!--方法无参数-->
<insert id="add1">
<![CDATA[
INSERT INTO student(id,name,sal) VALUES (1,"zhangsan",2000)
]]>
</insert> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<insert id="add2" parameterType="Student">
<![CDATA[
INSERT INTO student(id,name,sal) VALUES (#{id},#{name},#{sal})
]]>
</insert>
</mapper>

1.增加

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"> <!--namespace写成类的全限定名有好处,在Dao中方便-->
<mapper namespace="com.winner.entity.Student"> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<resultMap id="studentMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap> <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
<insert id="add" parameterType="Student">
<![CDATA[
INSERT INTO student(id,name,sal) VALUES (#{id},#{name},#{sal})
]]>
</insert>
</mapper>
public class StudentDao {

    /**
* 添加学生
*/
public void add(Student student) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
//事务开始(默认)
//读取StudentMapper.xml映射文件中的SQL语句
//insert的第一个参数:
// namespace写实体类的全限定名就是这个好处
sqlSession.insert(Student.class.getName() + ".add", 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,"zhansan",1000d));
}
}

2.根据ID查询学生

<!-- 
  根据ID查询学生
如果参数不是一个实体的话,只是一个普通变量,例如:int,double,String
这里的#{中间的变量名可以随便写},不过提倡就用方法的形参.
resultType是方法返回值类型
-->
<select id="findById" parameterType="int" resultType="Student">
SELECT id,name,sal FROM student WHERE id=#{id}
</select>
/**
* 根据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();
}
}

3.查询所有

<!-- 查询所有学生
理论上resultType要写List<Student>
但这里只需书写List中的类型即可,即只需书写Student的全路径名
-->
<select id="findAll" resultType="student">
SELECT id,name,sal FROM student
</select>
/**
* 查询所有学生
*/
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();
}
}

4.更新学生

<!-- 更新学生 -->
<update id="update" parameterType="Student">
  UPDATE student SET name=#{name},sal=#{sal} WHERE id=#{id}
</update>
/**
* 更新学生
*/
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();
}
}
Student student = studentDao.findById(3);
student.setName("wangwuwu");
studentDao.update(student);

5.删除

<!-- 删除学生 -->
<delete id="delete" parameterType="student">
  DELETE FROM student WHERE id = #{id}
</delete>
/**
* 删除学生
*/
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();
}
}
Student student = dao.findById(3);
dao.delete(student);

MyBatis的CRUD操作的更多相关文章

  1. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  2. 05 Mybatis的CRUD操作和Mybatis连接池

    1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...

  3. Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作

    开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...

  4. Mybatis:CRUD操作

    提示: Mapper配置文件的命名空间为对应接口包名+接口名字,这个经常会忘记和搞错的!! select标签 在接口中编写三个查询方法 //获取全部用户List<User> selectU ...

  5. MyBatis学习01(初识MyBatis和CRUD操作实现)

    1.初识MyBatis 环境说明: jdk 8 + MySQL 5.7.19 maven-3.6.1 IDEA 学习前需要掌握: JDBC MySQL Java 基础 Maven Junit 什么是M ...

  6. mybatis中crud操作范例

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  7. java之mybatis之使用mybatis实现crud操作

    目录结构: 1.封装 mybatis 的工具类: MybatisUtil.java public class MybatisUtil { private static SqlSessionFactor ...

  8. SSM框架之Mybatis(2)CRUD操作

    Mybatis(2)CRUD 1.基于代理Dao实现CRUD操作 使用要求: 1.持久层接口(src\main\java\dao\IUserDao.java)和持久层接口的映射配置(src\main\ ...

  9. Mybatis的CRUD案例

    一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...

随机推荐

  1. jbpm3.2中jbpm.jpdl.mysql.sql文件运行报错的问题

    这是一个很久之前遇到的问题,就是用从官网下下载的jbpm组件,它的jbpm.jpdl.mysql.sql不能正常运行.其原因是该sql文件中有一句语句有错误.现在附上正确的jbpm.jpdl.mysq ...

  2. .Net Core 中的包、元包与框架(Packages, Metapackages and Frameworks)

    包,元包与框架 本文翻译自 Packages, Metapackages and Frameworks. .Net Core 是一种由 NuGet 包组成的平台.一些产品体验受益于代码包的细粒度定义, ...

  3. template_11实参演绎

    1,演绎过程匹配类型A(来自实参的类型),参数化类型P(行参参数声明)如果被声明的参数是一个引用声明g(T& )那么P就是所引用类型T:f(T)中P就是所声明的参数类: decay指从数组和函 ...

  4. 普通树(有根树)C++

    对于普通树实现的细节包括 1 树结点的结构体 2 初始化及删除树结点(关注内存泄露) 3 递归先序遍历 4 通过关键值的查询操作,返回关键值的结点 5 凹入表实现 6 广义表实现 7 非递归先序遍历, ...

  5. OpenCV3添加滑动条和鼠标事件到图形界面

    鼠标事件和滑动条控制在计算机视觉和OpenCV中非常有用,使用这些控件,用户可以直接与图形界面交互,改变输入图像或者变量的属性值. /* In this section, we are going t ...

  6. js中的异常处理try...catch使用介绍

    在JavaScript可以使用try...catch来进行异常处理. 例如: try { foo.bar();} catch (e) { alert(e.name + ": " + ...

  7. ECSHOP购物流程收货人信息详细地址显示省市区

    方法一: 1.在flow.php中的 elseif ($_REQUEST['step'] == 'checkout') 中 $_SESSION['flow_consignee'] = $consign ...

  8. PHP 一个可以过滤非法脚本的函数

    这里提供一个过滤非法脚本的函数: function RemoveXSS($val) {     // remove all non-printable characters. CR(0a) and L ...

  9. ashx与验证码

    using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; us ...

  10. ExtJS4.2学习(七)EditorGrid可编辑表格(转)

    鸣谢地址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-14/176.html ------------- ...