mybatis高级(1)(入门回顾)
首先入门案例(并且拿到新增记录当前id)
1.创建mybatis-config.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>
<!-- 使用别名 -->
<typeAliases>
<!-- 为包下所有类使用别名,默认是简单类名 -->
<package name="cn.cnsdhzzl.entity" />
<!-- 对单个类,按类型名使用别名 -->
<!-- <typeAlias type="cn.cnsdhzzl.entity.Student" alias="student" /> -->
</typeAliases> <!-- 开发模式 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc的事务 -->
<transactionManager type="JDBC" />
<!-- 使用自带的连接池 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="**" />
<property name="password" value="**" />
</dataSource>
</environment>
</environments> <!-- 映射小配置 -->
<mappers>
<mapper resource="cn/cnsdhzzl/dao/StudentDAO.xml" />
</mappers>
</configuration>
2.创建分层搭建架构

3.创建StudentDAO.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.cnsdhzzl.dao">
<!-- 添加操作 -->
<insert id="insertStudent">
insert into student(id,name,address,sex)
values(seq_ssm.nextval,#{name},#{address},#{sex})
<selectKey keyProperty="id" resultType="int">
select
seq_ssm.currval from dual
</selectKey>
</insert> <!-- 删除的id名称随便写 -->
<delete id="deleteStudentById">
delete student where id=#{id}
</delete> <!-- 修改操作 -->
<update id="updateStudent">
update student set
name=#{name},address=#{address},sex=#{sex} where id=#{id}
</update> <!-- 返回集合要加resultType -->
<select id="findAll" resultType="student">
select * from student
</select> <!-- 方法一 -->
<select id="likeSelectByStr" resultType="student">
select * from student
where name like '%${value}%'
</select>
<!-- 方法二 -->
<!-- <select id="likeSelectByStr" resultType="student"> select * from student
where name like concat('%',#{stuname},'%') </select> --> <!-- 方法一 -->
<select id="likeSelectByEntity" resultType="student">
select * from student
where name like '%${name}%'
</select>
<!-- 方法二 -->
<!-- <select id="likeSelectByEntity" resultType="student"> select * from
student where name like concat('%',#{stuname},'%') </select> --> </mapper>
4.实现接口方法
@Override
public Integer addStudent(Student stu) {
SqlSession sqlSession = SessionUtil.getSqlSession();
int result = sqlSession.insert("insertStudent", stu); sqlSession.commit();
sqlSession.close(); System.out.println("保存结果" + result);
return result;
}
5.测试
@Test
public void frist() {
Student stu = new Student("BBB", "北京", "男");
StudentDaoImpl dao = new StudentDaoImpl();
dao.addStudent(stu);
System.out.println("成功");
}
结果图

注意事项:
语句后面不能加';'(分号),会报sql异常
mybatis高级(1)(入门回顾)的更多相关文章
- MyBatis高级篇之整合ehcache缓存框架
MyBatis高级篇之整合ehcache缓存框架 2017-09-01 0 Comments 1,671 Views 0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...
- 【Mybatis高级映射】一对一映射、一对多映射、多对多映射
前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...
- 8、web入门回顾/ Http
1 web入门回顾 web入门 1)web服务软件作用: 把本地资源共享给外部访问 2)tomcat服务器基本操作 : 启动: %tomcat%/bin/startup.bat 关闭: % ...
- mybatis高级映射(一对一,一对多)
mybatis高级映射 一对一关联映射 需求:查询订单信息,关联查询用户信息(一个订单对应一个用户) (1)通过resultType实现 sql语句: select orders.* , USER.u ...
- MyBatis(1)——快速入门
MyBatis 简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- (转) MyBatis(1)——快速入门
MyBatis 简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- MyBatis高级查询
-------------------------siwuxie095 MyBatis 高级查询 1.MyBatis 作为一个 ORM 框架,也对 SQL 的高级查询做了支持, MyBatis 高级查 ...
- Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建
目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...
- (转)MyBatis框架的学习(二)——MyBatis架构与入门
http://blog.csdn.net/yerenyuan_pku/article/details/71699515 MyBatis框架的架构 MyBatis框架的架构如下图: 下面作简要概述: S ...
随机推荐
- %----format 格式化字符串---- 生成器---- 迭代器
%方式格式化字符串 顺序传参数 o转换8进制x转换十六进制 tp1 = "i am %s" % "alex"tp2 = "i am %s age %d ...
- GreenDao关系建表
关系 在greenDAO,实体涉及使用一对一或一对多的关系.例如,如果要模拟一个1:greenDAOñ关系,你将有一个一对一和一对多的关系.但是,请注意,一对一和一对多的关系不是相互连接,所以你必须同 ...
- wf(七)(手把手包会)
这个demo中我们将用If/Else逻辑加到工作流用来展示不同的message通过自定义的条件. 如果name的字符数是奇数,第一个单词就输出“Greeting”否则输出“Hello”. 1. 在Sa ...
- 《利用python进行数据分析》读书笔记--第五章 pandas入门
http://www.cnblogs.com/batteryhp/p/5006274.html pandas是本书后续内容的首选库.pandas可以满足以下需求: 具备按轴自动或显式数据对齐功能的数据 ...
- 日志管理log4js的配置
以前就是在app.js 直接用,今天把它抽出来了. log4js.json { "appenders": [ { "type":"console&qu ...
- js 方法封装实例
(function(){ if(windows.Mr_2_B){windows.Mr_2_B={};} function trim(txt){return txt.replace(/(^\s*|(\s ...
- highcharts php请求mysql返回json数据作为数据源进行制图
直接上代码 [官方文档请参见http://www.highcharts.com/docs/working-with-data/getting-data-across-domains-jsonp] [实 ...
- 30-React JSX IN DEPTH
JSX IN DEPTH JSX 从根本上说,JSX只是提供了语法糖React.createElement(component, props, ...children)的功能.以下JSX代码: < ...
- VC++2013出现bug: 无法打开源文件“stdafx.h”
VC++2013出现bug: 无法打开源文件“stdafx.h” 1.首先需要把#include "stdafx.h"置于最头 2.在解决方案资源管理器中添加以下几个文件(附图下)
- 美国 ZIP Code 一览表
今天给大家提供美国的Zip Code的原因是大家在注册国外的账号时,需要提供这个Zip Code,因为一般美国的服务默认是面向美国的,甚至是仅支持美国. 以下提供一些美国的zip code 列表. 邮 ...