思路

搭建环境→导入Mybati→编写代码→测试

1.搭建环境

  搭建数据库

  新建项目

    注意事项

  

 

 

   删除src文件  

  导入依赖

    

<!--        Mybatis驱动-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>

<!-- mysql驱动,为了连接数据库-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>

<!-- junit测试用-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.创建模块

  new一个module(普通Maven项目)

  编写mybati核心配置文件(在resource中编写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核心配置文件-->

<!--MyBatis核心配置文件-->
<configuration>

<!-- <typeAliases>-->
<!-- <package name="com.zhaoyang.pojo"/>-->

<!-- </typeAliases>-->

<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- mysql8版本与5版本driver不相同-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"/> <property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>

<!-- 每一个Mapper.XML都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper resource="com/zhaoyang/dao/UserMapper.xml"/>
</mappers>

</configuration>

  编写工具类

    

public class MybatisUtils {
//3.创建建造者对象
private static SqlSessionFactory sqlSessionFactory;

static {
try {
//使用Mybatis第一步:获取sqlSessionFactory对象
//1.指定文件所在的位置 :类搜多的路径
String resource = "mybatis-config.xml";
//2.读取配置文件信息,转换为java 字节输入流丢向
InputStream inputStream = Resources.getResourceAsStream(resource);
//4.创建工厂对象
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}

//有了sqlSessionFactory,可以从中获得SqlSession实例
//SqlSession 完全包含了面向数据库执行SQL命令所需的方法
public static SqlSession getSqlSession(){
//5.创建sqlSession对象 会话对象
return sqlSessionFactory.openSession();
}


}
3.编写代码

   实体类:pojo

   Dao接口

public interface UserMapper {
//获取全部用户
List<User> getUserList();

//根据ID查询用户
User getUserById(int id);

//根据用户名查密码
User getUserPWD(String name);

//插入用户
int addUser(User user);

//修改用户
int updateUser(User user);

//删除用户
int deleteUser(int id);

}

   接口实现类

    

<?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">

<!--绑定一个对应的Dao/Mapper接口,不能用/必须用.-->
<mapper namespace="com.zhaoyang.dao.UserMapper">

<!-- 查询语句-->
<select id="getUserList" resultType="com.zhaoyang.pojo.User">
select * from mybatis.user
</select>

<!-- 根据ID查询用户-->
<select id="getUserById" parameterType="int" resultType="com.zhaoyang.pojo.User">
select * from mybatis.user where id = #{id}
</select>

<!-- 根据用户名查询密码-->
<select id="getUserPWD" parameterType="String" resultType="com.zhaoyang.pojo.User">
select * from mybatis.user where name = #{name}
</select>

<!-- 插入用户 对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.zhaoyang.pojo.User">
insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd});
</insert>

<!-- 修改用户-->
<update id="updateUser" parameterType="com.zhaoyang.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>

测试:
  
@Test
public void test(){
//第一步,获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();

for (User user : userList) {
System.out.println(user.toString ());
}

sqlSession.close();

}

注意点:

  报错:org.apache.ibatis.binding.BindingException: Type interface com.zhaoyang.dao.UserMapper is not known to the MapperRegistry.

 解决办法:在mybatix-config.xml文件中添加如下代码
每一个Mapper.XML都需要在Mybatis核心配置文件中注册
<mappers>
<mapper resource="com/zhaoyang/dao/UserMapper.xml"/>
</mappers>



在pom.xml文件中添加如下代码
<!--    Maven静态资源过滤问题
让maven在导出项目时,将我们的配置及资源导入classes文件
防止资源导出问题
-->
<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>

可能会遇到的问题:
  配置文件没有注册:增加Mapper
  绑定接口错误
  方法名不对
  返回类型不对:注意sql返回的类型,修改接收结果的类型
  Maven导出资源问题:增加buil



 

posted @ 2022-01-21 22:40  朝阳的博客  阅读(112)  评论(0编辑  收藏  举报