MyBatis入门程序

1,核心的配置文件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">
            <!-- 使用jdbc事务管理-->
            <transactionManager type="JDBC" />
            <!-- 数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="config/User.xml"/><!--这里的路径是相对于classes的,不是本文件的路径-->
    </mappers>

</configuration>

2,对应数据库表的实体类的配置文件User.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">
<mapper namespace="po">
    <!-- 根据id获取用户信息 -->
    <select id="findUserById" parameterType="int" resultType="po.User">
        select * from user where id = #{id}
    </select>
    <!-- 自定义条件查询用户列表 -->
    <select id="findUserByUsername" parameterType="java.lang.String"
            resultType="po.User">   <!--po是命名空间,即它是哪个包的哪个文件夹里的-->
        select * from user where username like '%${value}%'
    </select>
</mapper>

3,实体类和测试类的书写

public class TestFirst {

        //会话工厂
        private SqlSessionFactory sqlSessionFactory;

        @Before
        public void createSqlSessionFactory() throws IOException {
            // 配置文件
            String resource = "config/SqlMapConfig.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);

            // 使用SqlSessionFactoryBuilder从xml配置文件中创建SqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder()
                    .build(inputStream);

        }
        // 根据 id查询用户信息
        @Test
        public void testFindUserById() {
            // 数据库会话实例
            SqlSession sqlSession = null;
            try {
                // 创建数据库会话实例sqlSession
                sqlSession = sqlSessionFactory.openSession();
                // 查询单个记录,根据用户id查询用户信息
                User user = sqlSession.selectOne("po.findUserById", 1);  --findUserById是User.xml中Select元素的id
                // 输出用户信息
                System.out.println(user.getAge());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    }

注意:使用debug执行时,会出现连接数据库问题什么问题?????

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值