新建项目
-
新建一个普通的maven项目
-
删除src目录
-
导入maven依赖
<dependencies>
<!-- 添加mysql依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- 添加mybatis依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- 添加junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
4. 创建一个模块mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在mybatis核心配置文件中注册-->
<mappers>
<mapper resource="com/boss/dao/UserMapper.xml"/>
</mappers>
</configuration>
5.编写mybatis工具类 MybatisUtils
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用mybatis第一步,获取sqlSessionFactory对象
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = null;
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
6.编写代码
-
实体类
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 + '\'' +
'}';
}
}
-
Dao接口
public interface UserMapper {
User getUserById(int id);
List<User> getUserList();
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
-
接口实现类
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.boss.dao.UserMapper">
<!--结果集映射-->
<resultMap id="userMap" type="user">
<!-- column数据库中的字段 property实体类中的属性-->
<result property="id" column="id"/>
<result column="name" property="name"/>
<result property="password" column="pwd"/>
</resultMap>
<select id="getUserById" parameterType="int" resultMap="userMap">
select * from mybatis.user where id = #{id}
</select>
<select id="getUserList" resultType="user">
select * from mybatis.user
</select>
<insert id="addUser" parameterType="com.boss.pojo.User">
insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="com.boss.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>
可能会遇到的问题:
-
配置文件没有注册
-
绑定接口错误
-
方法名不对
-
返回类型不对
-
Maven导出资源问题
进行CRUD操作
namespace
namespace中的包名要和Dao/Mapper接口的包名一致!
select
选择,查询语句;
-
id:就是对应的namespace中的方法名;
-
resultType:sql语句执行的返回值;
-
parameterType:参数类型!
<select id="getUserList" resultType="com.boss.pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="com.boss.pojo.User">
select * from mybatis.user where id = #{id}
</select>
insert
<insert id="addUser" parameterType="com.boss.pojo.User">
insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
updata
<update id="updateUser" parameterType="com.boss.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id = #{id};
</update>
delete
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id}
</delete>
注意点
-
增删改需要提交事务。
分析错误
-
标签不要匹配错误
-
resource绑定mapper,需要使用路径!
-
程序配置文件必须符合规范!
-
输出的xml文件中存在乱码问题!
-
maven资源没有导出问题
7. 测试
public class UserDaoTest {
static Logger logger = Logger.getLogger(UserDaoTest.class);
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User userById = mapper.getUserById(1);
System.out.println(userById);
sqlSession.close();
}
@Test
public void getUserList(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.getUserList());
sqlSession.close();
}
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = (UserMapper) sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(5,"***","1234567"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"刘老六","123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(5);
sqlSession.commit();
sqlSession.close();
}
}