一、项目结构

二、代码实现

 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的更多相关文章

随机推荐

  1. 使用python3的typing模块提高代码健壮性

    前言:很多人在写完代码一段时间后回过头看代码,很可能忘记了自己写的函数需要传什么参数,返回什么类型的结果,就不得不去阅读代码的具体内容,降低了阅读的速度,加上Python本身就是一门弱类型的语言,这种 ...

  2. Kotlin(二) 函数定义

    1.不带参数,不返回值的函数 fun sum(){} 2.带参数,不带返回值的函数 fun sum(a:Int){} 3.带参数,带返回值的函数 fun sum(a:Int,b:Int) : Int{ ...

  3. pt-tcp-model

    http://blog.9minutesnooze.com/analyzing-http-traffic-tcpdump-perconas-pttcpmodel/ #获取200k个packets tc ...

  4. 这次真的忽略了一些ActiveMQ内心的娇艳

    好久没总结了,内心有点空虚了,所以今天主要给园里的朋友们分享一点儿这几天使用ActiveMQ过程中踩过的小坑,虽然说这东西简单易用,代码几行配置也就几行,问题不大但是后果有点严重,所以就要必要总结一下 ...

  5. 《Metasploit魔鬼训练营》第四章(上)

    p128 wmap 和昨天一样,我用这些漏洞扫描工具去扫testfire.net或者owaspbwa都扫不出漏洞!不明白! 补充:原来是网络不知道啥时候自己断了.连上后再次扫描就成功了:

  6. Centos下mongodb的安装与配置

    安装MongoDB的方法有很多种,可以源代码安装,在Centos也可以用yum源安装的方法. 1.准备工作 运行yum命令查看MongoDB的包信息 yum info mongodb-org (提示没 ...

  7. 分水岭 golang入坑系列

    第三式开篇语有些负面, 所以这里就不贴了.有兴趣的自己可以去看看 https://andy-zhangtao.gitbooks.io/golang/content/ .怒发冲冠,意气之作.看完就完了, ...

  8. 数据库—Mysql

    今天跟大家来聊聊Mysql,首先介绍一下它的历史: Mysql是一个关系型数据库管理系统,最先由瑞典的MySQL AB公司开发,后来被sun公司收购,后因sun公司又被Oracle公司收购,致使MyS ...

  9. Servlet之过滤器(Filter)和监听器(Listener)

    过滤器 过滤器是一个java组件,可以拦截发送至某个servelet,jsp页面或静态页面的请求,可以在响应发送到客户之前进行拦截 工作原理: 过滤器类必须实现 Filter 接口,包含的方法如下: ...

  10. ctrl+z 以后怎么恢复挂起的进程

    (1) CTRL+Z挂起进程并放入后台 (2) jobs 显示当前暂停的进程 (3) bg %N 使第N个任务在后台运行(%前有空格) (4) fg %N 使第N个任务在前台运行 默认bg,fg不带% ...