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. selector的理解

    对于nio这块最近几年一直就有关注,知道非阻塞,线程池,缓冲池,io的模式select,poll,epoll,甚至epoll中的et,lt. 但是最近才有时间实际看了看netty的源码,才发现原来se ...

  2. ArcGIS Server10.1授权文件

    3dengine,101,ecp.arcgis.server,01-jan-2020,UTE784S3EY83ZJKN0085 3dserver,101,ecp.arcgis.server,01-ja ...

  3. php入门变量之变量的间接引用、连接字符串和连接赋值运算符

    [1]变量的间接引用: <?php $a = 'b'; $$a = '123'; echo $b; ?> 上面的输出结果是123 我们可以看到在第二行代码中多了一个$,并通过指定的名称访问 ...

  4. 6个好用的Web开发工具

    在过去的几年间,涌现出了很多Web开发工具,它们大多还是比较吸引人的,方便了我们的工作.我们可以学习一下这些新东西,短时间就可以拓宽思路(PHP100推荐:学习10分钟,改变你的程序员生涯).这些应用 ...

  5. Spark Streaming揭秘 Day27 Job产生机制

    Spark Streaming揭秘 Day27 Job产生机制 今天主要讨论一个问题,就是除了DStream action以外,还有什么地方可以产生Job,这会有助于了解Spark Streaming ...

  6. DB2分区表删除和添加分区

    1.数据库版本 2.具体procedure DROP PROCEDURE DB2USER.TOOLS_PARTITION_TABLE_SHOW (VARCHAR ()); )) /********** ...

  7. 64位Python安装PIL

    写个小程序需要安装PIL,但是官网只有32位,无法找到64位安装路径.根据网上教程自行编译,但是由于VS版本问题总是提示“Python error: Unable to find vcvarsall. ...

  8. JavaScript 常用方法总结

    经常使用的 JS 方法,今天记下,以便以后查询 /* 手机类型判断 */ var BrowserInfo = { userAgent: navigator.userAgent.toLowerCase( ...

  9. 通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

    1.   前言 很多人都在使用myEclipse,很多公司也都使用hibernate框架,老版本的hibernate中,由于没有annotation,我们需要写两个文件来维护表与对象的关系,写一个类, ...

  10. Sybase 导入导出命令

    BCP命令详解 BCP是SYBASE公司提供专门用于数据库表一级数据备份的工具. 一般存放在所安装的ASE或者Open Client 的BIN目录中. 可执行文件名称为bcp.EXE 参数列表如下:( ...