一、项目结构
二、代码实现
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]