MyBatis_CURD
一、项目结构

二、代码实现
package com.jmu.bean;
public class Student {
private Integer id;
private String name;
private double score;
private int age;
public Student() {
super();
}
public Student(String name, int age ,double score) {
super();
this.name = name;
this.score = score;
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", score=" + score + ", age=" + age + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.jmu.dao; import java.util.List;
import java.util.Map; import com.jmu.bean.Student; public interface IStudentDao {
void insertStudent (Student student);
void insertStudentCacheId(Student student);//插入后获取 void deleteStudentById(int id);
void updateStudent(Student student); List<Student> selectAllStudents();//查询所有
Map<String,Object> selectAllStudentsMap();
Student selectStudentById(int id); //根据id查询
List<Student> selectStudentsByName(String name);//模糊查询
}
package com.jmu.dao; import static org.hamcrest.CoreMatchers.nullValue; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.persistence.MapKey; 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 com.jmu.bean.Student;
import com.jmu.utils.MybatisUtils; public class StudentDaoImpl implements IStudentDao { private InputStream inputStream;
private SqlSession sqlSession; @Override
public void insertStudent(Student student) {
// TODO Auto-generated method stub
try {
/*
* // 1、加载主配置文件 inputStream =
* Resources.getResourceAsStream("mybatis-config.xml"); //
* 2、创建SqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = new
* SqlSessionFactoryBuilder().build(inputStream); sqlSession =
* sqlSessionFactory.openSession(); // 4、相关操作
*/
sqlSession = MybatisUtils.getSqlSession();
sqlSession.insert("insertStudent", student);
sqlSession.commit();
} finally { if (sqlSession != null) {
sqlSession.close();
}
}
} @Override
public void insertStudentCacheId(Student student) {
// TODO Auto-generated method stub
try { sqlSession = MybatisUtils.getSqlSession();
sqlSession.insert("insertStudentCacheId", student);
sqlSession.commit();
} finally { if (sqlSession != null) {
sqlSession.close();
}
}
} @Override
public void deleteStudentById(int id) {
// TODO Auto-generated method stub
try { sqlSession = MybatisUtils.getSqlSession();
sqlSession.update("deleteStudentById", id);
sqlSession.commit();
} finally { if (sqlSession != null) {
sqlSession.close();
}
}
} @Override
public void updateStudent(Student student) {
// TODO Auto-generated method stub
try { sqlSession = MybatisUtils.getSqlSession();
sqlSession.update("updateStudent", student);
sqlSession.commit();
} finally { if (sqlSession != null) {
sqlSession.close();
}
}
} @Override
public List<Student> selectAllStudents() {
// TODO Auto-generated method stub
List<Student> students =new ArrayList<Student>();
try {
sqlSession=MybatisUtils.getSqlSession();
students=sqlSession.selectList("selectAllStudents"); } finally {
if (sqlSession!=null) {
sqlSession.close(); }
} return students;
} @Override
public Map<String, Object> selectAllStudentsMap() {
// TODO Auto-generated method stub
Map<String, Object> map=new HashMap<>();
try {
sqlSession=MybatisUtils.getSqlSession();
map=sqlSession.selectMap("selectAllStudents", "name");
} finally {
// TODO: handle finally clause
if (sqlSession!=null) {
sqlSession.close();
}
}
return map;
} @Override
public Student selectStudentById(int id) {
// TODO Auto-generated method stub
Student student=null;
try {
sqlSession=MybatisUtils.getSqlSession();
student=sqlSession.selectOne("selectStudentById", id); } finally {
// TODO: handle finally clause
if (sqlSession!=null) {
sqlSession.close(); }
}
return student;
} @Override
public List<Student> selectStudentsByName(String name) {
// TODO Auto-generated method stub
List<Student> students =new ArrayList<Student>();
try {
sqlSession=MybatisUtils.getSqlSession();
students=sqlSession.selectList("selectStudentByName",name); } finally {
if (sqlSession!=null) {
sqlSession.close(); }
}
return students;
} }
<?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="com.jmu.dao">
<insert id="insertStudent" parameterType="Student">
insert into
student(name,age,score) values(#{name},#{age},#{score})
</insert>
<insert id="insertStudentCacheId">
insert into student(name,age,score) values(#{name},#{age},#{score})
<selectKey resultType="int" keyProperty="id" order="AFTER">
select @@identity
</selectKey>
</insert>
<delete id="deleteStudentById">
delete from student where id=#{XXX}<!--这里的#{} 仅仅是个占位符,里边放什么都行 -->
</delete>
<update id="updateStudent">
update student set name=#{name},age=#{age},score=#{score} where id=#{id}
</update>
<select id="selectAllStudents" resultType="Student">
select id,name,age,score from student
</select>
<select id="selectStudentById" resultType="Student">
select id,name,age,score from student where id=#{JJJ}
</select>
<select id="selectStudentByName" resultType="Student">
<!-- select id,name,age,score from student where name like CONCAT('%',#{XXX},'%') -->
<!-- select id,name,age,score from student where name like '%王二%' -->
<!-- select id,name,age,score from student where name like '%${value}%' --><!-- 不存在sql注入 风险,但是效率低 -->
select id,name,age,score from student where name like '%' #{XXX} '%'<!--常用 -->
</select>
</mapper>
package com.jmu.utils; import java.io.IOException;
import java.io.InputStream; 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 {
private static SqlSessionFactory sqlSessionFactory; public static SqlSession getSqlSession() { try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
if (sqlSessionFactory == null) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); }
return sqlSessionFactory.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null; }
}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=root
jdbc.password=123456
<?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>
<properties resource="jdbc_mysql.properties"> </properties>
<typeAliases >
<package name="com.jmu.bean" />
</typeAliases>
<!--配置运行环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url"
value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<!--注册映射文件 -->
<mappers>
<mapper resource="com/jmu/dao/mapper.xml" />
</mappers>
</configuration>
package com.jmu.test; import static org.junit.Assert.*; import java.util.List;
import java.util.Map; import org.junit.Before;
import org.junit.Test; import com.jmu.bean.Student;
import com.jmu.dao.IStudentDao;
import com.jmu.dao.StudentDaoImpl; public class MyTest {
private IStudentDao dao; @Before
public void Before() {
dao = new StudentDaoImpl();
} @Test
public void test01() {
Student student = new Student("明明",19,87.9);
System.out.println("插入前:student="+student);
dao.insertStudent(student);
System.out.println("插入后:student="+student);
}
//插入后获取
@Test
public void test02(){
Student student =new Student("明明",23,99.5);
System.out.println("插入前:student="+student);
dao.insertStudentCacheId(student);
System.out.println("插入后:student="+student);
}
@Test
public void test03(){
dao.deleteStudentById(25);
}
@Test
public void test04(){
Student student=new Student("红酒",23,93.5);
student.setId(28);
dao.updateStudent(student);
}
@Test
public void test05(){
List<Student> students=dao.selectAllStudents();
for(Student student : students){
System.out.println(student);
}
}
@Test
public void test06(){
Map<String, Object> map = dao.selectAllStudentsMap();
System.out.println(map.get("王维"));
}
@Test
public void test07(){
Student student=dao.selectStudentById(33);
System.out.println(student);
}
@Test
public void test08(){
List<Student> students = dao.selectStudentsByName("王二");
for (Student student : students) {
System.out.println(student);
}
} }
插入前:student=Student [id=null, name=明明, score=87.9, age=19]
DEBUG [main] - ==> Preparing: insert into student(name,age,score) values(?,?,?)
DEBUG [main] - ==> Parameters: 明明(String), 19(Integer), 87.9(Double)
DEBUG [main] - <== Updates: 1
插入后:student=Student [id=null, name=明明, score=87.9, age=19]
插入前:student=Student [id=null, name=明明, score=99.5, age=23]
DEBUG [main] - ==> Preparing: insert into student(name,age,score) values(?,?,?)
DEBUG [main] - ==> Parameters: 明明(String), 23(Integer), 99.5(Double)
DEBUG [main] - <== Updates: 1
DEBUG [main] - ==> Preparing: select @@identity
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 1
插入后:student=Student [id=118, name=明明, score=99.5, age=23]
DEBUG [main] - ==> Preparing: delete from student where id=?
DEBUG [main] - ==> Parameters: 25(Integer)
DEBUG [main] - <== Updates: 0
DEBUG [main] - ==> Preparing: update student set name=?,age=?,score=? where id=?
DEBUG [main] - ==> Parameters: 红酒(String), 23(Integer), 93.5(Double), 28(Integer)
DEBUG [main] - <== Updates: 1
DEBUG [main] - ==> Preparing: select id,name,age,score from student
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 95
Student [id=17, name=王维, score=93.5, age=23]
Student [id=18, name=王维, score=93.5, age=23]
Student [id=19, name=王维, score=93.5, age=23]
Student [id=20, name=王维, score=93.5, age=23]
Student [id=21, name=王维, score=93.5, age=23]
Student [id=22, name=王维, score=93.5, age=23]
Student [id=23, name=王维, score=93.5, age=23]
Student [id=24, name=王维, score=93.5, age=23]
Student [id=26, name=王维, score=93.5, age=23]
Student [id=27, name=王维, score=93.5, age=23]
Student [id=28, name=红酒, score=93.5, age=23]
Student [id=29, name=安抚王二, score=93.5, age=23]
Student [id=30, name=王维一, score=93.5, age=23]
Student [id=31, name=王维二, score=93.5, age=23]
Student [id=32, name=王二分, score=93.5, age=23]
Student [id=33, name=王二维, score=93.5, age=23]
Student [id=34, name=王问维, score=93.5, age=23]
Student [id=35, name=王维, score=93.5, age=23]
Student [id=36, name=明明, score=87.9, age=19]
Student [id=37, name=明明, score=99.5, age=23]
Student [id=38, name=明明, score=87.9, age=19]
Student [id=39, name=明明, score=99.5, age=23]
Student [id=40, name=明明, score=87.9, age=19]
Student [id=41, name=明明, score=99.5, age=23]
Student [id=42, name=明明, score=87.9, age=19]
Student [id=43, name=明明, score=99.5, age=23]
Student [id=44, name=明明, score=87.9, age=19]
Student [id=45, name=明明, score=99.5, age=23]
Student [id=46, name=明明, score=87.9, age=19]
Student [id=47, name=明明, score=99.5, age=23]
Student [id=48, name=明明, score=87.9, age=19]
Student [id=49, name=明明, score=99.5, age=23]
Student [id=50, name=明明, score=87.9, age=19]
Student [id=51, name=明明, score=99.5, age=23]
Student [id=52, name=明明, score=87.9, age=19]
Student [id=53, name=明明, score=99.5, age=23]
Student [id=54, name=明明, score=87.9, age=19]
Student [id=55, name=明明, score=99.5, age=23]
Student [id=56, name=明明, score=87.9, age=19]
Student [id=57, name=明明, score=99.5, age=23]
Student [id=58, name=明明, score=87.9, age=19]
Student [id=59, name=明明, score=99.5, age=23]
Student [id=60, name=明明, score=87.9, age=19]
Student [id=61, name=明明, score=99.5, age=23]
Student [id=62, name=明明, score=87.9, age=19]
Student [id=63, name=明明, score=99.5, age=23]
Student [id=64, name=明明, score=87.9, age=19]
Student [id=65, name=明明, score=99.5, age=23]
Student [id=66, name=明明, score=87.9, age=19]
Student [id=67, name=明明, score=99.5, age=23]
Student [id=68, name=明明, score=87.9, age=19]
Student [id=69, name=明明, score=99.5, age=23]
Student [id=70, name=明明, score=87.9, age=19]
Student [id=71, name=明明, score=99.5, age=23]
Student [id=72, name=明明, score=87.9, age=19]
Student [id=73, name=明明, score=99.5, age=23]
Student [id=74, name=明明, score=87.9, age=19]
Student [id=75, name=明明, score=99.5, age=23]
Student [id=76, name=明明, score=87.9, age=19]
Student [id=77, name=明明, score=99.5, age=23]
Student [id=78, name=明明, score=87.9, age=19]
Student [id=79, name=明明, score=99.5, age=23]
Student [id=80, name=明明, score=87.9, age=19]
Student [id=81, name=明明, score=99.5, age=23]
Student [id=82, name=明明, score=87.9, age=19]
Student [id=83, name=明明, score=99.5, age=23]
Student [id=84, name=明明, score=87.9, age=19]
Student [id=85, name=明明, score=99.5, age=23]
Student [id=86, name=明明, score=87.9, age=19]
Student [id=87, name=明明, score=99.5, age=23]
Student [id=88, name=明明, score=87.9, age=19]
Student [id=89, name=明明, score=99.5, age=23]
Student [id=90, name=明明, score=87.9, age=19]
Student [id=91, name=明明, score=99.5, age=23]
Student [id=92, name=明明, score=87.9, age=19]
Student [id=93, name=明明, score=99.5, age=23]
Student [id=94, name=明明, score=87.9, age=19]
Student [id=95, name=明明, score=99.5, age=23]
Student [id=100, name=明明, score=87.9, age=19]
Student [id=101, name=明明, score=99.5, age=23]
Student [id=104, name=明明, score=87.9, age=19]
Student [id=105, name=明明, score=99.5, age=23]
Student [id=106, name=明明, score=87.9, age=19]
Student [id=107, name=明明, score=87.9, age=19]
Student [id=108, name=明明, score=87.9, age=19]
Student [id=109, name=明明, score=87.9, age=19]
Student [id=110, name=明明, score=87.9, age=19]
Student [id=111, name=明明, score=87.9, age=19]
Student [id=112, name=明明, score=87.9, age=19]
Student [id=113, name=明明, score=87.9, age=19]
Student [id=114, name=明明, score=99.5, age=23]
Student [id=115, name=明明, score=87.9, age=19]
Student [id=116, name=明明, score=99.5, age=23]
Student [id=117, name=明明, score=87.9, age=19]
Student [id=118, name=明明, score=99.5, age=23]
DEBUG [main] - ==> Preparing: select id,name,age,score from student
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 95
Student [id=35, name=王维, score=93.5, age=23]
DEBUG [main] - ==> Preparing: select id,name,age,score from student where id=?
DEBUG [main] - ==> Parameters: 33(Integer)
DEBUG [main] - <== Total: 1
Student [id=33, name=王二维, score=93.5, age=23]
DEBUG [main] - ==> Preparing: select id,name,age,score from student where name like '%王二%'
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 3
Student [id=29, name=安抚王二, score=93.5, age=23]
Student [id=32, name=王二分, score=93.5, age=23]
Student [id=33, name=王二维, score=93.5, age=23]
MyBatis_CURD的更多相关文章
随机推荐
- C#实现的Redis扩展项目(二次封装)
Redis在当下的互联网项目当中的普及率我想都不用多说了,本文要介绍的這个项目是基于我对Redis理解程度的基础上写的一个公共类库项目,希望对各位童鞋有所帮助,也欢迎各位对我都内容提出更好的意见. 由 ...
- 基于Cef内核的多店铺登录器(含源码)
公司是做电商的,在速卖通平台上开了若干店铺,每天都需要登录店铺打理,如:发货提交.获取运单号等.多个店铺的情况下,同时使用浏览器就会非常繁琐,如:要记住帐户名和密码,还要在不同店铺间切换.如果能够制作 ...
- 【Kafka源码】KafkaConsumer
[TOC] KafkaConsumer是从kafka集群消费消息的客户端.这是kafka的高级消费者,而SimpleConsumer是kafka的低级消费者.何为高级?何为低级? 我们所谓的高级,就是 ...
- mysql数据库第一弹
mysql(一) sql语句 sql是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 在使用它时,只需要 ...
- java获取泛型信息
总结一下java中获取与泛型相关的信息的知识,不如说是使用方法.网上也有很多类似的优秀文章,这里主要做一个知识的总结.通过反射获取泛型信息的常见例子: //bean package testProje ...
- css3 ajax加载进度线
最近想了想ajax加载时的进项,便着手写了这个,我想css3的支持度已经够了 <button onclick="start()">button</button&g ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...
- Minecraft
描述 Minecraft是一个几乎无所不能的沙盒游戏,玩家可以利用游戏内的各种资源进行创造,搭建自己的世界. 在Minecraft中,基本的建筑元素是边长为1个单位的立方体,Tony想用N个这种小立方 ...
- Class 与 Style 绑定
将 v-bind 用于 class 和 style 时,Vue.js 做了专门的增强.表达式结果的类型除了字符串之外,还可以是对象或数组. 绑定 HTML Class 对象语法 <div cla ...
- (四):C++分布式框架——状态中心模块
(四):C++分布式框架--状态中心模块 上篇:(三):C++分布式实时应用框架--系统管理模块 技术交流合作QQ群:436466587 欢迎讨论交流 版权声明:本文版权及所用技术归属smartguy ...