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 ...
随机推荐
- The project: project which is referenced by the classpath, does not exist.
有个java工程运行时报The project: project which is referenced by the classpath, does not exist.错误 上网查解决方案,说是将 ...
- RIO-SEIO水泵流量表
http://rio-seio.com/TW/products_pumps/Rio_plus.html 流量表:
- questasim仿真控制——breakpoint断点
在使用questasim或者modelsim仿真时,如果需要控制仿真时间长度,一般在vsim中使用 run xxxxms/us等命令. 但是有时候不好估计仿真多长时间才能得到所有希望观察的结果,这个时 ...
- Mvc public virtual DbQuery<TResult> Include("")
参数 path 类型:System.String要在查询结果中返回的相关对象列表(以点号分隔). 返回值 类型:System.Data.Entity.Infrastructure.DbQuery< ...
- Python-4 变量、字符串
#1 变量 1)使用前 先赋值 2)命名 字母.数字.下划线 且 不由数字开头 3)大小写不等 4)名字=值 5)尽量选取专业的名字 #2 字符串(文本) 1)字符串两边加引号 2)转义字符 \(反斜 ...
- Node.js 自学之旅
学习基础,JQuery 原生JS有一定基础,有自己一定技术认知(ps:原型链依然迷糊中.闭包6不起来!哎!) 当然最好有语言基础,C#,java,PHP等等.. 最初学习这个东西的原因很简单,在园子里 ...
- centos 7 挂载大硬盘
对硬盘sdb进行分区 parted -a optimal /dev/sdb 使用GPT格式 mklabel gpt 建立一个主分区 mkpart primary - 显示分区信息 print 退出 q ...
- android 非activity如何得到布局文件 (java文件中获取布局文件)
Android中得到布局文件对象有两种方式第一种,在Activity所在类中this.getLayoutInflater().inflater(R.layout.布局文件名,null);第二种,在非A ...
- PAT 03-树1 树的同构 (25分)
给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后 ...
- FusionCharts中仪表盘相关属性
上上周用FusionCharts做了几个报表,里面有个仪表盘,当时查属性查疯了,现在把相关的一些属性记下来,方便以后查找. -------------------------仪表盘重要属性解析---- ...