CRUD
1、namespace命名空间
namespace中的包名要和Dao/mapper接口的包名一致!!!
2、select
选择,查询语句;
id:就是对应的namespace中的方法名
resultType:sql语句执行的返回值!!!
parameterType:参数类型
一、创建maven工程
选择file->project
选择Maven
-
创建工程名 :Mabatis-Study,下面的Location自动生成
出现此界面
删除src
-
点击src->main->resources,创建file 命名为mybatis-config.xml
二、配置Maven环境
导入父工程
<?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>org.example</groupId>
<artifactId>Mybatis-Study</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 导入依赖 -->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
三、创建数据库
以MySQL新建数据库mybatis -->创建表user -->填入id name pwd
四、创建实体类与接口
这样就可以找到url的地址
实体类:
package com.mybatis.pojo;
public class User {
private int id;
private String name;
private String pwd;
//实体类
public User() {
}
//无参
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 + '\'' +
'}';
}
}
Mapper接口
package com.mybatis.dao;
import com.mybatis.pojo.User;
import java.util.List;
//接口
public interface UserMapper {
//查询全部用户
List<User> getUserList();
}
接口实现类
<?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.mybatis.dao.UserMapper">
<!-- 查询语句-->
<select id="getUserList" resultType="com.mybatis.pojo.User" >
select * from mybatis.user;
</mapper>
在dao包中创建UserMapper类文件
package com.mybatis.dao;
import com.mybatis.pojo.User;
import java.util.List;
//接口
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户 根据id int id
User getUserById(int id);
}
五、配置核心文件
在resources新建一个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代表核心配置文件-->
<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?userSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在Mybaties核心配置文件中注册-->
<mappers>
<mapper resource="com/mybatis/dao/UserMapper.xml"/>
</mappers>
</configuration>
-
编写mybatis工具类
-
建立dao包和util包,在util包中键MybatisUtils
package com.mybatis.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;
//SqlSessionFactory -->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//获取 第一步:SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。jdbc connection
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
六、测试
在test包下建立com.mybatis.dao包,-》UserMapperTest.java
添加测试
package com.mybatis.dao;
import com.mybatis.pojo.User;
import com.mybatis.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void test(){
//第一部:获取sqlsession对象
SqlSession sqlSession= MybatisUtils.getSqlSession();
//执行SQL
UserMapper userDao=sqlSession.getMapper(UserMapper.class);
List<User> userList=userDao.getUserList();
for (User user:userList){
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
1.1查询功能
在UserMapperjava中添加查询id用户
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户 根据id int id
User getUserById(int id);
}
在UserMapper.xml中添加select语句
<select id="getUserById" parameterType="int" resultType="com.mybatis.pojo.User">
/*写select语句*/
select*from mybatis.user where id=#{id}
在UserMapperTest中添加
@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();
}
查询结果运行成功
1.2插入功能
在UserMapper.xml中添加对象属性
<!--对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.mybatis.pojo.User" >
insert into mybatis.user(id,name , pwd) values (#{id},#{name},#{pwd});
</insert>
在UserMapper.java中添加insert
//insert一个用户
int addUser(User user);
在UserMapperTest中添加
//增删改需要提交事务
@Test
public void addUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
int res= mapper.addUser(new User( 4,"哈哈","125678"));
if(res>0){
System.out.println("插入成功!!!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
运行成功
1.3修改功能
在UserMapper.java中添加修改用户
//修改用户
int updateUser(User user);
在UserMapper.xml中进行修改
<update id="updateUser" parameterType="com.mybatis.pojo.User">
update mybatis.user set name=#{name}, pwd=#{pwd} where id=#{id};
</update>
在UserMapperTest中添加修改
//修改
@Test
public void updateUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"呵呵","890890"));
sqlSession.commit();
sqlSession.close();
}
修改成功
1.4删除功能
在UserMapper.java中添加删除用户
//删除一个用户
int deleteUser(int id);
在UserMapper.xml中添加删除
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id= #{id};
</delete>
测试
//删除
@Test
public void deleteUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
运行成功