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 ...
随机推荐
- 压力测试工具JMeter入门教程
1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...
- MYSQL中存储过程的创建,调用及语法
MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库 ...
- angular的路由
AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web应用(single page web application,SPA). 下面 ...
- 关于去除Eclipse对JavaScript的验证
关于去除Eclipse对JavaScript的验证 在我们使用大量JavaScript作为一些UI或其他组件来使用时,很多情况下,明明引用的这些JavaScript是可以正常使用的,但Eclipse却 ...
- CSS篇
一.盒子模型: 标准模式和混杂模式(IE).在标准模式下浏览器按照规范呈现页面:在混杂模式下,页面以一种比较宽松的向后兼容的方式显示.混杂模式通常模拟老式浏览器的行为以防止老站点无法工作. CSS盒子 ...
- .net面试题集锦
1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...
- 运行两个以上tomcat的设置及内存设置
运行两个或以上tomcat的设置方法 1.解决端口冲突问题设置方法很简单,修改conf/server.xml配置文件中的3个端口即可.默认端口:8005.8080.8009.一般情况位置如下:**** ...
- R:incomplete final line found by readTableHeader on
报错: In read.table("abc.txt", header = T) : incomplete final line found by readTableHead ...
- test lab ~ triangle test by using junit and coverage
first set up a new folder as your test class place, and then let your package in test class folder b ...
- python3 cgi传数据
用cgi向文本或者数据库写入数据 1.开启cgi服务 python -m http.server --cgi port[端口可选默认为8000] update.py代码 #coding:utf8im ...