数据持久化
Dao层、Service层、Controller层
建表语句
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) NOT NULL,
`pwd` VARCHAR(30) NOT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES
(1,'张三','1234'),
(2,'李四','1234'),
(3,'王五','1234')
创建maven项目,导入相关依赖
<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.0modelVersion>
<groupId>MybatisStudygroupId>
<artifactId>MybatisStudyartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>mybatis01module>
modules>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
dependency>
dependencies>
project>
编写Mybatis相关配置文件
<configuration>
<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/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="11111111"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="UserMapper.xml"/>
mappers>
configuration>
编写Mybatis工具类
package util;
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 java.io.IOException;
import java.io.InputStream;
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
try{
//使用mybatis第一步获取SqlSessionFactory对象
String resource="mybatis-conf.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}catch (IOException e)
{
e.printStackTrace();
}
}
//有了SqlSessionFactory就可以获取SqlSession了
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
依据数据库编写实体类
package pojo;
public class User {
private int id;
private String name;
private String pwd;
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 String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
编写相关实体操作接口
package dao;
import pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
编写相关配置文件,写出实现实体操作接口的sql语句
<mapper namespace="dao.UserDao">
<select id="getUserList" resultType="pojo.User">
select * from mybatis.user
select>
mapper>
调用相关方法
package Dao;
import dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import util.MybatisUtil;
import java.util.List;
public class UserDaoTest {
@Test
public void test()
{
//获取SqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
//执行sql
UserDao userDao=sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
package dao;
import pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
User getUserById(int id);
void addUser(User user);
void updateUser(User user);
void deleteUser(User user);
}
配置文件编写实现方法sql语句
<mapper namespace="dao.UserDao">
<select id="getUserList" resultType="pojo.User">
select * from mybatis.user;
select>
<select id="getUserById" parameterType="int" resultType="pojo.User">
select * from mybatis.user where id=#{id};
select>
<insert id="addUser" parameterType="pojo.User">
insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd});
insert>
<update id="updateUser" parameterType="pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
update>
<delete id="deleteUser" parameterType="pojo.User">
delete from mybatis.user where id=#{id};
delete>
mapper>
进行相应的调用
package Dao;
import dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import util.MybatisUtil;
import java.util.List;
public class UserDaoTest {
@Test
public void test()
{
//获取SqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
//执行sql
UserDao userDao=sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void getUserById()
{
SqlSession sqlSession=MybatisUtil.getSqlSession();
UserDao userDao=sqlSession.getMapper(UserDao.class);
User user=userDao.getUserById(1);
System.out.println(user);
sqlSession.close();
}
//增删改需要提交事务
@Test
public void addUser()
{
SqlSession sqlSession=MybatisUtil.getSqlSession();
UserDao userDao=sqlSession.getMapper(UserDao.class);
userDao.addUser(new User(4,"赵六","1234"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser()
{
SqlSession sqlSession= MybatisUtil.getSqlSession();
UserDao userDao=sqlSession.getMapper(UserDao.class);
userDao.updateUser(new User(4,"赵六","123412"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser()
{
SqlSession sqlSession=MybatisUtil.getSqlSession();
UserDao userDao=sqlSession.getMapper(UserDao.class);
userDao.deleteUser(new User(4,"赵六","123412"));
sqlSession.commit();
sqlSession.close();
}
}