0.思路:搭建环境—>导入mybatis—>编写代码—>测试
参考文档:mabatis中文文档
1.搭建环境
1.首先通过maven导入jar包
<dependencies>
<!-- mysql驱动-->
<!-- <dependency>-->
<!-- <groupId></groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
2.创建一个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>
<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?useSSL=true&characterEncoding=utf8&useUnicode=true"/>
<property name="username" value="root"/>
<property name="password" value="lin"/>
</dataSource>
</environment>
</environments>
</configuration>
具体的配置要自己修改
3.编写代码…
4.org.apache.ibatis.binding.BindingException: Type interface dao.UserDao is not known to the MapperRegistry.
第一个常见的错误:进行解决
<!--每一个Mapper.xml都需要在mybatis核心配置文件中注册! -->
<mappers>
<mapper resource="dao/UserMapper.xml">
</mapper>
第二个常见的错误:
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
原因:由于maven的约定大于配置,我们之后可能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
而且
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&characterEncoding=utf8&useUnicode=true"/>
上面的useSSL一定要等于false,否则就会报错,切记(具体原因不知道为啥)
5.CRUD(增删改查)
id:就是对应的namespace中的方法名
resultType:sql语句执行的返回值
parameterType:参数类型
然后在dao接口和xml文件中进行
//接口的代码
package dao;
import pojo.User;
import java.util.List;
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户
User getUserById(int id);
//insert一个用户
int adduser(User user);
//修改用户
int updateUser(User user);
//删除一个用户
int deleteUser(int id);
}
xml的代码
<?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 namespace="dao.UserMapper">
<!--查询语句-->
<select id="getUserList" resultType="pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="pojo.User">
select * from mybatis.user where id = #{id}
</select>
<insert id="adduser" parameterType="pojo.User" >
insert into mybatis.user (companyname, username, pwd) VALUES (#{companyname},#{username},#{pwd});
</insert>
<update id="updateUser" parameterType="pojo.User" >
update mybatis.user set companyname=#{companyname},username=#{username},pwd=#{pwd} where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id};
</delete>
</mapper>