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 ...
随机推荐
- U盘安装CentOS 7.0
U盘安装CentOS 7.0 由于学习需要centos环境,so上网下载centos安装镜像,发现版本已经到7.0了,(╮(╯▽╰)╭,上次折腾还是6.4的版本呢,)花了一点时间下载下来按照装6.4的 ...
- sql server 常见问题笔记
1.关于复制类型 快照发布:发布服务器按预定的时间间隔向订阅服务器发送已发布数据的快照. 事务发布:在订阅服务器收到已发布数据的初始快照后,发布服务器将事务流式传输到订阅服务器. 对等发布:对等发布支 ...
- CentOS 6.5下静默安装oracle
本例: 通过SSH远程连接云主机,上传oracle11g安装包,在centos6.5上无图形化界面静默安装oracle11g. 涉及工具及环境: 1.本地环境windows7+ssh远程连接工具xSh ...
- 【转载】Understand the serialVersionUID
If you have ever implemented Serializable interface, you must encounter this warning message The ser ...
- window.frame
定义和用法 frames 属性返回窗口中所有命名的框架. 该集合是 Window 对象的数组,每个 Window 对象在窗口中含有一个框架或 <iframe>.属性 frames.leng ...
- Linux安装MySql.Data for mono
wget http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-6.8.3-noinstall.zipunzip m ...
- tiny中文乱码问题,不过仅适用于windows,所以xml不可以出现中文
我是在SetAttribute() 函数之前使用的 SetAttribute(const char* name,const char * _value) 首先得到了一个CString 类型的变量 st ...
- /etc/passwd&/etc/shadow文件分析
/etc/passwd该目录存储的是操作系统用户信息,该文件为所有用户可见.给linux系统添加一个帐号:useradd -g mysql -d /home/test -m test(:新建一个用户t ...
- java后台调用url无协议
url格式不正确,可能有"www.baidu.com" "这个不能有 // 下载pdf public void downpdf(String URL, String ...
- 转:ServletContext,ActionContext,ServletActionContext
ServletContext ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 javax.servlet.ServletCont ...