简介
2.第一个Mybatis程序
搭建数据库
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,'狂神','123455'),
(2,'张三','123423'),
(3,'李四','23423234'),
新建项目
1.新建一个普通的maven项目
2 删除src目录
3 导入maven依赖
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2,2 创建一个模块
- 创建mybatis的核心配置文件
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root"/>
<property name="password" value="love819099119"/>
</dataSource>
</environment>
</environments>
</configuration>
- 编写mybatis工具类
//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的实例了
//SqlSession完全包含了面向数据库执行SQL命令所需的所有方法。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
2.3 编写代码
实体类
package com.hao.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
Dao接口
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
接口实现类由原来的UserDaoImpl转变为一个Mapper配置文件
<mapper namespace="com.hao.dao.UserDao">
<!-- select查询语句-->
<select id="getUserList" resultType="com.hao.pojo.User">
select * from mybatis.user
</select>
</mapper>
2.4 、测试
你们可能遇到的问题
绑定接口错误
方法名不对
返回类型不对
maven导出资源
3 CRUD
1 、namespace
namespace中的包名要和 Dao/mapper 接口的包名一致
2、select
选择,查询语句
- id:就是对应的namespace中的方法名;
- resultType:Sql语句执行的返回值!
- parameterType :参数类型!
1 编写接口
//根据ID查询用户
User getUserById(int id);
2 编写对应的mapper中的sql语句
<select id="getUserById" parameterType="int" resultType="com.hao.pojo.User">
select * from mybatis.user where id =#{id}
</select>
3测试
@Test
public void getUserById(){
SqlSession sqlSession =MybatisUtils.getSqlSession();
UserDao mapper =sqlSession.getMapper(UserDao.class);
User user =mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
3 Insert
<insert id="addUser" parameterType="com.hao.pojo.User">
insert into mybatis.user (id,name,pwd) values(#{id},#{name},#{pwd});
</insert>
4 Update
<update id="updateUser" parameterType="com.hao.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id =#{id};
</update>
5 Delete
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id};
</delete>
注意点
- 增删改需要提交事务