20 - Mybatis学习(2)- DAO
程序员文章站
2022-06-17 16:06:24
...
1 MyBatis的Dao编写 【一般不用,有更多好方式】
package com.tzb.dao;
import com.tzb.model.User;
public interface IUserDao {
public void save(User user);
public User findUserById(int id);
}
package com.tzb.dao;
import com.tzb.model.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class UserDaoImpl implements IUserDao {
private SqlSessionFactory ssf;
public UserDaoImpl() {
}
public UserDaoImpl(SqlSessionFactory ssf) {
this.ssf = ssf;
}
@Override
public void save(User user) {
SqlSession session = ssf.openSession();
session.insert("insertUser", user);
session.commit();
session.close();
}
@Override
public User findUserById(int id) {
SqlSession session = ssf.openSession();
User user = session.selectOne("selectUser",id);
session.close();
return user;
}
}
2 MyBatis的Dao编写【mapper代理方式实现】
- Mapper代理的开发方式,程序员只需要编写mapper接口(相当于dao接口)即可。Mybatis会自动的为mapper接口生成动态代理实现类。
- 要实现mapper代理的开发方式,需要遵循一些开发规范。
开发规范
- mapper接口的全限定名要和mapper映射文件的namespace的值相同。
- mapper接口的方法名称要和mapper映射文件中的statement的id相同;
- mapper接口的方法参数只能有一个,且类型要和mapper映射文件中statement的parameterType的值保持一致。
- mapper接口的返回值类型要和mapper映射文件中statement的resultType值或resultMap中的type值保持一致;
通过规范式的开发mapper接口,可以解决原始dao开发当中存在的问题:
- 模板代码已经去掉;
- 剩下去不掉的操作数据库的代码,其实就是一行代码。这行代码中硬编码的部分,通过第一和第二个规范就可以解决。
- 第一步
重新写个UserMapper配置文件和定义mapper映射文件UserMapper.xml(内容同Users.xml,除了namespace的值),放到新创建的目录mapper下。
package com.tzb.mapper;
import com.tzb.model.User;
public interface UserMapper {
/**
*
* @param user
* @return 返回受影响的行数
*/
public int save(User user);
public User findUserById(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">
<mapper namespace="com.tzb.mapper.UserMapper">
<insert id="save" parameterType="com.tzb.model.User">
<!-- 这里的占位写模型的属性-->
INSERT INTO user (username,sex,birthday,address)
VALUE (#{username},#{sex},#{birthday},#{address});
</insert>
<select id="findUserById" parameterType="int" resultType="com.tzb.model.User">
SELECT * FROM user WHERE id = #{id};
</select>
</mapper>
- 添加映射配置文件
SqlMapConfig.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的环境信息 -->
<environments default="development">
<environment id="development">
<!-- 配置JDBC事务控制,由mybatis进行管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源,采用dbcp连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_test?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<!--<mapper resource="com/tzb/sqlmap/User.xml"></mapper>-->
<mapper resource="com/tzb/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
- 测试类
import com.tzb.mapper.UserMapper;
import com.tzb.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
public class Demo4 {
private SqlSession session;
@Before
public void before() throws IOException {
System.out.println("before.......获取 session");
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
session = factory.openSession();
}
@After
public void after() {
System.out.println("after.......关闭 session");
session.close();
}
@Test
public void test1() throws IOException {
// 代理
UserMapper userMapper = session.getMapper(UserMapper.class);
System.out.println(userMapper.findUserById(1));
System.out.println("----------------------------------------");
User user = new User("Mary", "女", new Date(), "广州");
userMapper.save(user);
session.commit();
}
}