MyBatis的CRUD操作
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操作的更多相关文章
- 【MyBatis】MyBatis实现CRUD操作
1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...
- 05 Mybatis的CRUD操作和Mybatis连接池
1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...
- Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...
- Mybatis:CRUD操作
提示: Mapper配置文件的命名空间为对应接口包名+接口名字,这个经常会忘记和搞错的!! select标签 在接口中编写三个查询方法 //获取全部用户List<User> selectU ...
- MyBatis学习01(初识MyBatis和CRUD操作实现)
1.初识MyBatis 环境说明: jdk 8 + MySQL 5.7.19 maven-3.6.1 IDEA 学习前需要掌握: JDBC MySQL Java 基础 Maven Junit 什么是M ...
- mybatis中crud操作范例
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- java之mybatis之使用mybatis实现crud操作
目录结构: 1.封装 mybatis 的工具类: MybatisUtil.java public class MybatisUtil { private static SqlSessionFactor ...
- SSM框架之Mybatis(2)CRUD操作
Mybatis(2)CRUD 1.基于代理Dao实现CRUD操作 使用要求: 1.持久层接口(src\main\java\dao\IUserDao.java)和持久层接口的映射配置(src\main\ ...
- Mybatis的CRUD案例
一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...
随机推荐
- Android Studio生成APK自动追加版本号
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/android-apk-studio-generated-automatically-appends-a-ve ...
- 使用JavaScript实现简单的输入校验
HTML页面代码: <!doctype html> <html lang="en"> <head> <meta charset=" ...
- ubuntu vim 7.4 编译安装
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4137402.html 1.到官网 http://www.vim.org/download.p ...
- php parallel
http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/ http://stackoverflow.com/question ...
- 一些dos命令
MS DOS 命令大全 一.基础命令 1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含 ...
- php 时间转化总结
iQuery插件datepicker获取的时间函数为"月月/天天/年年年年"(以04/21/2015为例)的形式 (1)转化为2015-21-04形式:$start = date( ...
- 《C和指针》 读书笔记 -- 第11章 动态内存分配
1.C函数库提供了两个函数,malloc和free,分别用于执行动态内存分配和释放,这些函数维护一个可用内存池. void *malloc(size_t size);//返回指向分配的内存块起始位置的 ...
- 编码错误设置错误报 "SyntaxError: Non-ASCII character '/xe6' "
无意中碰到键盘导致一段处理中文拼音的 python 代码跑起来报了个错 “SyntaxError: Non-ASCII character ‘/xe6' " 看了下是注释 # coding: ...
- Android类库常用类库一览
在Android SDK中包括很多包文件,通过了解这些包的功能也有助于了解可以开发的功能. 在Android类库中,各种包写成android.*的方式,重要包的描述如下所示: android.app ...
- 1027 Colors in Mars (20)
#include <stdio.h> #include <map> using namespace std; int main() { int R,G,B,i; map< ...