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 ...
随机推荐
- nyoj 708 ones 动态规划
http://acm.nyist.net/JudgeOnline/problem.php?pid=708 状态转移方程的思路:对于一个数N,可以是N - 1的状态+1 得到,另外,也可以是(n / 2 ...
- 物料主数据MRP4中的独立/集中
转自悲守穷庐 http://blog.itpub.net/12287/viewspace-681569/ 从按订单还是按库存来考虑. (1)独立集中为空,即又上层决定独立集中情况 (2)独立集中为1: ...
- ADVDAV驱动
// ADV7179 register configuration array for PAL mode ADI_DEV_ACCESS_REGISTER ADV7179_Cfg[]={ {ADV717 ...
- nginx学习笔记
我的工作环境是 Debian . 在 Debian 上安装 ngingx 和其他 linux 安装基本相同. 在配置 hello world 之前,没有头绪,看了很多资料.最后 "https ...
- ubuntu 13.10 svn工具 rabbitvcs 安装
ubuntu 版本:13.10:桌面模式默认:unity :文件管理器:nautilus
- Openlayers简介
OpenLayers 是由MetaCarta公司开发的,用于WebGIS客户端的 JavaScript包,目前的最高版本是2.5 V,通过BSD License 发行.它实现访问地理空间数据的方法都符 ...
- java反射,ReflectUtils
public class ReflectUtils { /** * 通过构造函数实例化对象 * @param className 类的全路径名称 * @param parameterTypes 参数类 ...
- WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default i
jdbc连接数据库候,对数据进行访问,访问正常当出现如下警告: WARN: Establishing SSL connection without server's identity verifica ...
- spring资料
spring的官方文档还是很全面的: http://link.zhihu.com/?target=http%3A//docs.spring.io/spring/docs/current/spring- ...
- 用OSSIM轻松分析网络设备日志
用OSSIM轻松分析网络设备日志 基于插件的日志收集与处理模式,使得用户可以轻松的利用OSSIM来分析异构网络环境下的各种网络设备日志,下面展示一些硬件设备日志的实例,我们在RAW LOG界面里,搜索 ...