1. namespace
namespace中的包名要和接口的包名一致
2.select
选择/查询语句
id:就是对应的namespace中的方法名
resultType:Sql语句执行的返回值!
paramteType:参数类型!
列子:
1.编写接口
//查询全部用户
List<User> getUserList();
2.编写对应的sql中的语句
<!--查询语句-->
<select id="getUserList" resultType="com.yz.pojo.User">
select *from mybatis.user;
</select>
3.测试
@Test
public void getUserById(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.commit();
sqlSession.close();
}
//增删改查需要提交事务
1.select
<!--查询语句-->
<select id="getUserList" resultType="com.yz.pojo.User">
select *from mybatis.user;
</select>
<select id="getUserById" parameterType="int" resultType="com.yz.pojo.User">
select *from mybatis.user where id=#{id}
2.insert(插入)
<!--对象中的属性可以直接取出来-->
<insert id="addUser" parameterType="com.yz.pojo.User">
insert into mybatis.user(id, name, pwd) values (#{id},#{name },#{pwd})
</insert>
3.update(修改)
<update id="upadteUser" parameterType="com.yz.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
4.delete(删除)
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id}
</delete>
完整步骤:
导入依赖:
父依赖:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.yz</groupId>
<artifactId>mybatis-study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-01</module>
</modules>
<dependencies>
<!--导入依赖-->
<dependency>
<!--mysql驱动-->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<!-- 在build中配置resources , 来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
创建数据库:
create database `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT(20) NOT null primary key,
`name` varchar(30) default NULL,
`pwd` varchar(30) default NUll
)engine=InnoDB default CHARSET=utf8;
insert INTO `user`(`id`,`name`,`pwd`) values
(1,'狂神','123456'),
(2,'张三','123456'),
(3,'李四','123456')
Uer.java(实体类)
package com.yz.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User(String s, String 哈哈, String pwd) {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = 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 + '\'' +
'}';
}
}
MybatisUtils.java (工具类)
package com.yz.utils;
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;
//构建 SqlSessionFactory-->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用mybatis第一步:获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// org.apache.ibatis.session.SqlSession 提供了在数据库执行 org.apache.ibatis.jdbc.SQL 命令所需的所有方法。
// 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
UserDao接口
package com.yz.dao;
import com.yz.pojo.User;
import java.util.List;
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据ID查询用户
User getUserById(int id);
//insert 一个用户(插入)
int addUser(User user);
//修改用户
int upadteUser(User user);
//删除用户
int deleteUser(int id);
}
UserMapper.xml(SQL语句)
<?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">
<!--namespace==绑定一个对应的Dao/mapper接口-->
<mapper namespace="com.yz.dao.UserMapper">
<!--查询语句-->
<select id="getUserList" resultType="com.yz.pojo.User">
select *from mybatis.user;
</select>
<select id="getUserById" parameterType="int" resultType="com.yz.pojo.User">
select *from mybatis.user where id=#{id}
</select>
<!--对象中的属性可以直接取出来-->
<insert id="addUser" parameterType="com.yz.pojo.User">
insert into mybatis.user(id, name, pwd) values (#{id},#{name },#{pwd})
</insert>
<update id="upadteUser" parameterType="com.yz.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id}
</delete>
</mapper>
测试:UerDao.java
package com.yz.dao;
import com.yz.pojo.User;
import com.yz.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test() {
//第一步:获得sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
//方式一:getMapper
UserMapper userdao = sqlSession.getMapper(UserMapper.class);
List<User> userList = userdao.getUserList();
//方式二:
// List<User> userList=sqlSession.selectList("com.yz.dao.UserDao.getUserList");
for (User user : userList) {
System.out.println(user);
}
} catch (Exception e) {
} finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void getUserById(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
//增删改查需要提交事务
@Test
public void addUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int result = mapper.addUser(new User(7,"tt","12345"));
if (result>0){
System.out.println("插入成功!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
mapper.upadteUser(new User(4,"呵呵呵","1234456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(0);
sqlSession.commit();
sqlSession.close();
}
}
注意:
增删改查需要提交事务
sqlSession.commit();