mybatis基于传统dao的开发方式
第一步:开发接口
public interface UserDao { public User getUserById(int id) throws Exception; public void insertUser(User user) throws Exception; }
第二步:设置实现类
public class UserDaoImpl implements UserDao { private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } public UserDaoImpl() { super(); } @Override public User getUserById(int id) throws Exception { //获取sqlSession,需要使用sqlSessionFactory SqlSession session = sqlSessionFactory.openSession(); try { User user = session.selectOne("selectUserById", id); return user; } finally{ session.close(); } } @Override public void insertUser(User user) throws Exception { //获取sqlSession,需要使用sqlSessionFactory SqlSession session = sqlSessionFactory.openSession(); try { session.insert("insertUser", user); session.commit(); } finally{ session.close(); } } }
第三步:设置对应的mapper文件
<?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="mybatis.mapper.UserMapper"> <!-- 根据id获取用户信息 --> <select id="selectUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> <!-- 向数据库插入一条数据 --> <insert id="insertUser" parameterType="user"> <selectKey keyProperty="id" resultType="int" order="AFTER"> select last_insert_id(); </selectKey> insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) </insert> </mapper>
加载mapper文件
<?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> <!-- 加载外部配置文件 --> <properties resource="db.properties"> </properties> <!-- 这个environments定义了我们的数据库的连接操作 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 通过mappers来指定加载我们对应的mapper文件 --> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
第五步:编写测试案例
public class UserDaoTest { private SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory; } @Test public void testGetUserById() throws Exception { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); UserDao userDao = new UserDaoImpl(sqlSessionFactory); User user = userDao.getUserById(1); System.out.println(user); } @Test public void testInsertUser() throws Exception { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); UserDao userDao = new UserDaoImpl(sqlSessionFactory); User user = new User(); user.setUsername("安茱莉"); user.setSex("0"); user.setAddress("好莱坞38号"); userDao.insertUser(user); } }
存在的问题
- Dao方法体存在重复代码:通过SqlSessionFactory创建SqlSession,调用SqlSession的数据库操作方法
- 调用sqlSession的数据库操作方法需要指定statement的id,这里存在硬编码,不得于开发维护。
通过接口代理的方式来开发dao
官方重点推荐这种方式
开发规范:
Mapper接口开发方法只需要编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper接口开发需要遵循以下规范:
1、 Mapper.xml文件中的namespace与mapper接口的类路径相同。
2、 Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、 Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
4、 Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
第一步:定义接口
public interface UserMapper { // 根据id查找用户 public User findUserById(int id) throws Exception; // 插入用户信息 public void insertUser(User user) throws Exception; }
第二步:定义mapper文件
<?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="mybatis.mapper.UserMapper"> <!-- 根据id获取用户信息 --> <select id="findUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> <!-- 向数据库插入一条数据 --> <insert id="insertUser" parameterType="user"> insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) </insert> </mapper>
第三步:加载mapper文件
<?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> <!-- 别名的配置 --> <typeAliases> <!-- 不区分大小写的 --> <!-- <typeAlias type="domain.User" alias="user"/> --> <!-- 扫描这个包下面的所有类,起别名,也是不区分大小写的 ,名字是类名不带包,推荐这种方式--> <package name="domain"/> </typeAliases> <!-- 加载外部配置文件 --> <properties resource="db.properties"> </properties> <!-- 这个environments定义了我们的数据库的连接操作 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 通过mappers来指定加载我们对应的mapper文件 --> <!-- 此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。 --> <mappers> <mapper resource="mapper.xml"/> <!-- <mapper class="mybatis.mapper.UserMapper"/> --> <!-- 加载包下所有mapper接口,要求接口名称和mapper映射文件名称相同,且放在同一个目录中。 --> <package name="mybatis.mapper"/> </mappers> </configuration>
第四步:开发测试代码
public class UserMapperTest { /** * 通过接口代理开发dao层的测试 * 调用getUserById方法,传递id参数 * 输出user对象到控制台 */ @Test public void findUserByIdTest() throws Exception{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(true); //mybatis会产生一个代理对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(2); System.out.println(user); sqlSession.close(); } ... /** * 抽取方法,保证项目中SQLSessionFactory的单例 */ private SqlSessionFactory getSqlSessionFactory() throws IOException { InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory; } }
补充:mybatis默认支持的别名:
别名 | 映射的类型 |
---|---|
_byte | byte |
_long | long |
_short | short |
_int | int |
_integer | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
decimal | BigDecimal |
bigdecimal | BigDecimal |
map | Map |
原文地址:https://www.cnblogs.com/jifengblog/p/9221324.html
时间: 2024-10-29 19:11:29