目录列表:
D:\code\smvc\mybatis-one>tree src /f
卷 软件 的文件夹 PATH 列表
卷序列号为 0000-CD08
D:\CODE\SMVC\MYBATIS-ONE\SRC
├─main
│ └─java
│ │ jdbc.properties
│ │ mybatis-config.xml
│ │
│ └─com
│ └─laolang
│ ├─domain
│ │ Student.java
│ │ StudentMapper.xml
│ │
│ ├─mapper
│ │ StudentMapper.java
│ │
│ ├─services
│ │ StudentService.java
│ │
│ └─util
│ MybatisUtil.java
│
└─test
└─java
└─com
└─laolang
└─service
StudentServiceTest.java
D:\code\smvc\mybatis-one>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.laolang.mybatis-study</groupId>
<artifactId>mybatis-one</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mybatis-one</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
</project>
mybatis配置
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis_study
username=root
password=root
mybatis-config.xml
<?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>
<!-- 加载 jdbc properties 文件 -->
<properties resource="jdbc.properties" />
<typeAliases>
<typeAlias alias="Student" type="com.laolang.domain.Student" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/laolang/domain/StudentMapper.xml" />
</mappers>
</configuration>
实体类及CRUD操作实现
com.laolang.domain.Student
package com.laolang.domain;
public class Student {
public Student() {
super();
}
public Student(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public Student(int id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", sex=" + sex + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
private int id;
private String name;
private int age;
private String sex;
}
com.laolang.domain.StudentMapper.xml
<?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.laolang.mapper.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
</resultMap>
<select id="selectAll" resultMap="StudentResult">
select id,name,age,sex from student
</select>
<select id="selectById" parameterType="int" resultType="Student">
select id,name,age,sex from student where id = #{id}
</select>
<select id="selectByName" parameterType="String" resultType="Student">
select id,name,age,sex from student where name = #{name}
</select>
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
insert into student ( name, age, sex ) values(#{name},#{age},#{sex})
</insert>
<update id="updateStudent" parameterType="Student">
update student set name = #{name} , age = #{age} , sex = #{age}
where id = #{id}
</update>
<delete id="deleteStudentById" parameterType="int">
delete from student where id = #{id}
</delete>
</mapper>
com.laolang.mapper.StudentMapper
package com.laolang.mapper;
import java.util.List;
import com.laolang.domain.Student;
public interface StudentMapper {
public List<Student> selectAll();
public Student selectById(int id);
public Student selectByName(String name);
public void insertStudent(Student student );
public void updateStudent(Student student );
public void deleteStudentById(int id );
}
com.laolang.services.StudentServices
package com.laolang.services;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.laolang.domain.Student;
import com.laolang.mapper.StudentMapper;
import com.laolang.util.MybatisUtil;
public class StudentService {
public List<Student> selectAll() {
SqlSession session = MybatisUtil.openSession();
try {
StudentMapper studentMapper = session
.getMapper(StudentMapper.class);
return studentMapper.selectAll();
} finally {
session.close();
}
}
public Student selectById(int id) {
SqlSession session = MybatisUtil.openSession();
try {
StudentMapper studentMapper = session
.getMapper(StudentMapper.class);
return studentMapper.selectById(id);
} finally {
session.close();
}
}
public Student selectByName(String name) {
SqlSession session = MybatisUtil.openSession();
try {
StudentMapper studentMapper = session
.getMapper(StudentMapper.class);
return studentMapper.selectByName(name);
} finally {
session.close();
}
}
public void insertStudent(Student stu) {
SqlSession session = MybatisUtil.openSession();
try {
StudentMapper studentMapper = session
.getMapper(StudentMapper.class);
studentMapper.insertStudent(stu);
session.commit();
} finally {
session.close();
}
}
public void updateStudent(Student student) {
SqlSession session = MybatisUtil.openSession();
try {
StudentMapper studentMapper = session
.getMapper(StudentMapper.class);
studentMapper.updateStudent(student);
session.commit();
} finally {
session.close();
}
}
public void deleteStudentById(int id) {
SqlSession session = MybatisUtil.openSession();
try {
StudentMapper studentMapper = session
.getMapper(StudentMapper.class);
studentMapper.deleteStudentById(id);
session.commit();
} finally {
session.close();
}
}
}
测试文件 :
StudentServiceTest
package com.laolang.service;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import com.laolang.domain.Student;
import com.laolang.services.StudentService;
public class StudentServiceTest {
@BeforeClass
public static void setUp() {
studentService = new StudentService();
}
@Ignore
@Test
public void testSelectAll() {
System.out.println("-->查询所有-----------");
List<Student> students = studentService.selectAll();
for (Student stu : students) {
System.out.println(stu);
}
System.out.println("---------------------------\n\n");
}
@Ignore
@Test
public void testSelectById() {
System.out.println("-->通过 id 查询-----------");
Student stu = studentService.selectById(1);
System.out.println(stu);
System.out.println("---------------------------\n\n");
}
@Ignore
@Test
public void testSelectByName() {
System.out.println("-->通过 name 查询-----------");
Student stu = studentService.selectByName("小代码");
System.out.println(stu);
System.out.println("---------------------------\n\n");
}
@Ignore
@Test
public void testInsertStudent() {
System.out.println("-->插入-----------");
String name = "天涯";
Student stu = new Student(name, 34, "男");
studentService.insertStudent(stu);
stu = studentService.selectByName(name);
System.out.println(stu);
System.out.println("---------------------------\n\n");
}
@Ignore
@Test
public void testUpdateStudent() {
System.out.println("-->修改-----------");
Student stu = studentService.selectById(1);
System.out.println();
stu.setName("xiaodaima");
studentService.updateStudent(stu);
stu = studentService.selectById(1);
System.out.println(stu);
System.out.println("---------------------------\n\n");
}
@Ignore
@Test
public void testDeleteStudentById() {
System.out.println("-->删除-----------");
List<Student> studentList = studentService.selectAll();
for( Student stu : studentList ){
System.out.println(stu);
}
studentService.deleteStudentById(7);
System.out.println("删除后:\n");
studentList = studentService.selectAll();
for( Student stu : studentList ){
System.out.println(stu);
}
System.out.println("---------------------------\n\n");
}
private static StudentService studentService;
}