MyBatis复习【简单配置CRUD】
这里的案例集成了log4j的日志框架,项目架构:

用到的jar文件

添加配置文件:mybatis-config.xml 和dao层配置文件StudentDao.xml
这里书写了个简单的案例仅为了说明问题
dao中有几个增删改查的抽象方法
/**
* 添加新的学生
* @param stu
* @return 返回 该insert语句成功后的自增列
* @throws IOException
*/
public int SaveInfo(Student stu) throws IOException; /**
* 根据id删除学生信息
* @param stuno
* @return
* @throws IOException
*/
public int DeleteInfo(int stuno) throws IOException; /**
* 根据学生对象 模糊查询学生信息
*/
public List<Student> getAllInfoByStudent(Student stu) throws IOException; /**
* 根据学生姓名 模糊查询学生信息
*/
public List<Student> getAllInfoByName(String stuName) throws IOException;
书写doa对应的配置文件
<?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.zym.mybatis.dao">
<!-- <select id="addStudent" parameterType="cn.zym.mybatis.entity.Student">
insert into Student(stuname,stuage) values(#{stuName},#{stuAge})
</select> -->
<insert id="addStudent" parameterType="Student">
insert into Student(stuname,stuage) values(#{stuName},#{stuAge})
<selectKey keyProperty="stuNo" resultType="_integer" order="AFTER">
<!-- oracle需要设置order为BEFORE :select seq_ssm.currval from dual(); -->
select @@identity
</selectKey>
</insert> <delete id="deleteInfo" parameterType="int">
delete from student where stuno=#{stuno}
</delete> <select id="getAllInfoByStudent" parameterType="Student" resultType="Student">
select * from student s where s.stuname like concat('%',#{stuName},'%')
</select> <select id="getAllInfoByName" resultType="Student">
select * from student s where s.stuname like '%${value}%'<!-- 此处若要使用${xxx}的写法,其值必须为"value" -->
</select> </mapper>
StudentDao.xml
在dao中使用的别名在大配置文件中设置别名:
<?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>
<!-- 配置cn.zym.mybatis.entity包下的所有bean的别名为简单类名; -->
<typeAliases>
<package name="cn.zym.mybatis.entity"/>
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/zhangyiming" />
<property name="username" value="zym" />
<property name="password" value="admin" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/zym/mybatis/dao/StudentDao.xml" />
</mappers>
</configuration>
Mybatis-config.xml
这两个文件中的头文件都可以到附带的pdf文档中获取 Getting started目录节点下可找到;
StudentDaoImpl
package cn.zym.mybatis.dao.impl; import java.io.IOException;
import java.io.Reader;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import cn.zym.mybatis.dao.IStudentDao;
import cn.zym.mybatis.entity.Student;
import cn.zym.mybatis.utils.MybatisUtils; public class StudentDaoImpl implements IStudentDao { /*@Override
public int SaveInfo(Student stu) throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
int insert = session.insert("addStudent",stu);
session.commit();
session.close();
return insert;
}*/ @Override
public int SaveInfo(Student stu) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
int insert = sqlSession.insert("addStudent",stu);
sqlSession.commit();
sqlSession.close();
return insert;
} @Override
public int DeleteInfo(int stuno) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
int delete = sqlSession.delete("deleteInfo",stuno);
sqlSession.commit();
return delete;
} @Override
public List<Student> getAllInfoByStudent(Student stu) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<Student> list = sqlSession.selectList("getAllInfoByStudent",stu);
return list;
} @Override
public List<Student> getAllInfoByName(String stuName) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<Student> list = sqlSession.selectList("getAllInfoByName",stuName);
return list;
} }
StudentDaoImpl
MybatisUtils
package cn.zym.mybatis.utils; import java.io.IOException;
import java.io.Reader; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtils {
static Reader reader =null; static{
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
}
static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
public static SqlSession getSqlSession(){
SqlSession session = factory.openSession();
return session; } }
MybatisUtils
测试code
package cn.zym.mybatis.test; import java.io.IOException;
import java.util.List; import org.junit.Before;
import org.junit.Test; import cn.zym.mybatis.dao.IStudentDao;
import cn.zym.mybatis.dao.impl.StudentDaoImpl;
import cn.zym.mybatis.entity.Student; public class Mytest {
IStudentDao dao ;
@Before
public void initalData(){
dao= new StudentDaoImpl();
} /**
* 模糊 姓名 查询学生信息
*/
@Test
public void queryStudentByBean() throws IOException{
List<Student> list = dao.getAllInfoByStudent(new Student("zym2",));
for (Student student : list) {
System.out.println(student);
}
System.out.println("ok!");
} /**
* 模糊 姓名 查询学生信息
*/
@Test
public void queryStudentByString() throws IOException{
List<Student> list = dao.getAllInfoByName("");
for (Student student : list) {
System.out.println(student);
}
System.out.println("ok!");
} /**
* 删除学生信息
*/
@Test
public void deleteStudent() throws IOException{
dao.DeleteInfo();
System.out.println("ok!");
} /**
* 添加学生信息
* @throws IOException
*/
@Test
public void addStudent() throws IOException{
IStudentDao dao = new StudentDaoImpl();
Student stu = new Student("zym", );
System.out.println("保存前:"+stu);
dao.SaveInfo(stu);
System.out.println("save ok!");
System.out.println("保存后:"+stu);
}
}
test code
MyBatis复习【简单配置CRUD】的更多相关文章
- Mybatis实现简单的CRUD(增删改查)原理及实例分析
Mybatis实现简单的CRUD(增删改查) 用到的数据库: CREATE DATABASE `mybatis`; USE `mybatis`; DROP TABLE IF EXISTS `user` ...
- spring+mybatis的简单配置示例
简单代码结构: //Book.java package com.hts.entity; public class Book { private String id; private String bo ...
- Springboot项目搭建(1)-创建,整合mysql/oracle,druid配置,简单的CRUD
源码地址:https://github.com/VioletSY/article-base 1:创建一个基本项目:https://blog.csdn.net/mousede/article/detai ...
- Mybatis 复习 Mybatis 配置 Mybatis项目结构
pom.xml文件已经贴在了文末.该项目不使用mybatis的mybatis-generator-core,而是手写Entities类,DaoImpl类,CoreMapper类 其中,Entities ...
- Mybatis缓存(1)--------系统缓存及简单配置介绍
前言 Mybatis的缓存主要有两种: 系统缓存,也就是我们一级缓存与二级缓存: 自定义的缓存,比如Redis.Enhance等,需要额外的单独配置与实现,具体日后主要学习介绍. 在这里主要记录系统缓 ...
- springboot + mybatis 的项目,实现简单的CRUD
以前都是用Springboot+jdbcTemplate实现CRUD 但是趋势是用mybatis,今天稍微修改,创建springboot + mybatis 的项目,实现简单的CRUD 上图是项目的 ...
- MyBatis 使用简单的 XML或注解用于配置和原始映射
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .My ...
- Mybatis框架的简单配置
Mybatis 的配置 1.创建项目(当然,这是废话) 2.导包 下载mybatis-3.2.0版:https://repo1.maven.org/maven2/org/mybatis/mybatis ...
- Hello Mybatis 01 第一个CRUD
What's the Mybatis? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google c ...
随机推荐
- jquery中获取元素的几种方式小结
1 从集合中通过指定的序号获取元素 html: 复制代码代码如下: <div> <p>0</p> <p>1</p> <p>2&l ...
- Laravel学习笔记(四)数据库 数据库迁移案例
创建迁移 首先,让我们创建一个MySql数据库“Laravel_db”.接下来打开app/config目录下的database.php文件.请确保default键值是mysql: return arr ...
- js 处理 html 标签转义 处理json中含有的ascii 编码
function escape2Html(str) { var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&am ...
- XMLHttpRequest简单总结
一.概念 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是能够: 在不重新加载页面的情况下更新网页 在页面已加载后从服务器请求数据 在页面已加载后从 ...
- Qt 环境下的mapx控件-------2
今天花了一天的时间去查找mapx相关的资料,但是到最后想要的东西还是一无所获,不过还是学到了很多东西.下面以大家分享一下: mapx软件的安装:下载后安装mapx软件,成功后会在安装路径下存在acti ...
- oracle加并行参数PARALLEL
select /*+ PARALLEL(t,4) */ * from table1
- linux错误码
1.通过代码输出错误码以及其代表的含义 具体可以参考errno和os模块 errno.errorcode os.strerror(n) # -*- coding:utf8 -*- import os ...
- 许小年:宁可踏空,不可断粮<转>
http://www.daonong.com/g/25/xsqy/2014/0716/51074.html 文│许小年 中欧国际工商学院教授 为什么我们企业的创新能力长期处于低水平呢? 深入观察,内心 ...
- pod install后无反应
参考这篇文章 http://akinliu.github.io/2014/05/03/cocoapods-specs-/
- Hibernate注解使用以及Spring整合
Hibernate注解使用以及Spring整合 原文转自:http://wanqiufeng.blog.51cto.com/409430/484739 (1) 简介: 在过去几年里,Hibernate ...