2.第一个Mybatis程序
2.1 搭建环境
2.2 创建一个模块
2.3 编写代码
创建工具类(封装创建Sqlsession的类工具包)
public class mybaitsTool{
public static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
创建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"/>
<!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="admin123" />
</dataSource>
</environment>
</environments>
<!-- mapping文件路径配置 -->
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
步骤: 实体类 Dao接口 接口实现类(使用xml的方式)
实体类:
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public User(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Dao接口
public interface UseMapper {
//根据ID查询用户
User getuserbyid(int id);
//插入一个用户
int insert(User user);
//修改一个用户
int update(User user);
//删除一个用户
int delete(int id);
}
Dao接口实现类
<?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">
<mapper namespace="dao.UseMapper">
<select id="getuserList" resultType="pojo.User">
select * from user
</select>
<select id="getuserbyid" parameterType="int" resultType="pojo.User">
select * from user where id = #{id}
</select>
<insert id="insert" parameterType="pojo.User" >
insert into user(id,name) values (#{id},#{name})
</insert>
<update id="update" parameterType="pojo.User" >
update user set name=(#{name}) where id=(#{id})
</update>
<delete id="delete" parameterType="int" >
delete from user where id=(#{id})
</delete>
</mapper>
创建测试单元:
public class AppTest {
//查询
@Test
public void demo1() {
SqlSession sqlSession = mybaitsTool.sqlSessionFactory.openSession();
UseMapper mapper = sqlSession.getMapper(UseMapper.class);
User user = mapper.getuserbyid(2);
System.out.println(user);
}
//插入
@Test
public void demo2() {
SqlSession sqlSession = mybaitsTool.sqlSessionFactory.openSession();
UseMapper mapper = sqlSession.getMapper(UseMapper.class);
// User user=new User(5,"陈其","asddasd");
mapper.insert(new User(4,"陈其12"));
sqlSession.commit();
sqlSession.close();
}
//修改
@Test
public void demo3()
{
SqlSession sqlSession=mybaitsTool.sqlSessionFactory.openSession();
UseMapper mapper =sqlSession.getMapper(UseMapper.class);
mapper.update(new User(4,"陈其"));
sqlSession.commit();
sqlSession.close();
}
//删除
@Test
public void demo4()
{
SqlSession sqlSession=mybaitsTool.sqlSessionFactory.openSession();
UseMapper mapper = sqlSession.getMapper(UseMapper.class);
mapper.delete(5);
sqlSession.commit();
sqlSession.close();
}
}
万能Map(作用:为了修改某个内容时,只需要修改对应的内容即可,不许要添加额外的形参)
假设我们的实现类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map
//Map增加一个用户
int insert2(Map<String,Object> map);
<insert id="insert2" parameterType="pojo.User">
insert into user(id,pwd) values (#{userid},#{userpwd})
</insert>
//Map键值插入
@Test
public void demo5() {
SqlSession sqlSession = mybaitsTool.sqlSessionFactory.openSession();
UseMapper mapper = sqlSession.getMapper(UseMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userid",6);
map.put("userpwd","6789556");
mapper.insert2(map);
sqlSession.commit();
sqlSession.close();
常见错误:
1.数据库版本要和mysql驱动包的版本保持大版本一致,否则运行会出错
2.增改删操作需要提交事务 查不要
3.测试中使用的是接口的形参,不是实现类的