大鹏一日同风起,扶摇直上九万里。这篇文章主要讲述mybatis学习之路mapper代理开发方法-开发规范及实现相关的知识,希望能为你提供帮助。
1.在mapper.xml中namespace等于mapper接口得地址
< ?xml version="1.0" encoding="UTF-8"?> < !DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"2.mapper.java接口中的方法名字和mapper.xml中的statement即(findUserById)一致
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
< mapper namespace="com.jjt.ssm.mapper.TuserMapper">
< select id="findUserById" parameterType="int" resultType="com.jjt.ssm.pojo.Tuser">
SELECT * FROM tuser WHERE id=#{userid}
< /select>
< /mapper>
3.mapper.java接口中的方法输入参数类型和mapper.xml和statement的parameterType指定类型保持一致。
4.mapper.java接口中的返回类型和mapper.xml和statement的resultType指定类型保持一致。
import com.jjt.ssm.pojo.Tuser;
public interface TuserMapper {
public Tuser findUserById(int userid) throws Exception;
}
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>
< !-- 和spring整合后这个 environments 移除-->
< environments default="development">
< environment id="development">
< transactionManager type="JDBC" />
< !-- 配置数据库连接信息 -->
< dataSource type="POOLED">
< !-- value属性值引用db.properties配置文件中配置的值 -->
< property name="driver" value="https://www.songbingjia.com/android/com.mysql.jdbc.Driver" />
< property name="url" value="https://www.songbingjia.com/android/jdbc:mysql://127.0.0.1:3306/jiajitao?useUnicode=true& amp; characterEncoding=UTF-8" />
< property name="username" value="https://www.songbingjia.com/android/root" />
< property name="password" value="https://www.songbingjia.com/android/root" />
< /dataSource>
< /environment>
< /environments>
< mappers>
< !-- < mapper resource="sqlmap/Tuser.xml"/> -->
< mapper resource="mapper/TuserMapper.xml"/>
< /mappers>
< /configuration>
import java.io.InputStream;
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.Before;
import org.junit.Test;
import com.jjt.ssm.pojo.Tuser;
public class TuserMapperTest {
SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws Exception {
String resource = "sqlMapConfig.xml";
// 配置文件读取
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindUserById() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
TuserMapper userMapper = sqlSession.getMapper(TuserMapper.class);
Tuser user = userMapper.findUserById(1);
System.out.println(user.getUsername());
}
【mybatis学习之路mapper代理开发方法-开发规范及实现】}
推荐阅读
- android中init.rc文件的解析问题
- Android如何设置圆角按钮
- Android KK后为何工厂模式下无法adb 无法重新启动机器 ()
- HDU 6030 Happy Necklace
- Android_1.1
- android电话状态的监听
- Python SciPy初学者教程和示例(如何使用SciPy())
- 如何检查TensorFlow版本(使用6种不同的方法)
- Istio是什么(架构、特性、优势和挑战介绍指南)