1、mybatis学习——mybatis入门程序

 


一、原始方式:

  1.创建employee

  

  2.项目中创建相对应的bean

  3.导入基本jar包

   4.将日志文件配置导入

复制代码
# Global logging configuration
#在开发环境下日志级别要设置成DEBUG,生产环境设置成ERROR或者INFO
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
复制代码

  5.创建数据源文件

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisstudy
jdbc.username=root
jdbc.password=123

  6.创建mybatis全局配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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>
        <!-- resource:引入类路径下的资源
            url:引入网络路径或者磁盘路径下的资源 -->
      <properties resource="dbconfig.properties"></properties>
     
     <environments default="development">
         <environment id="development">
             <transactionManager type="JDBC"/>
                 <dataSource type="POOLED">
                     <property name="driver" value="${jdbc.driver}"/>
                     <property name="url" value="${jdbc.url}"/>
                     <property name="username" value="${jdbc.username}"/>
                     <property name="password" value="${jdbc.password}"/>
                 </dataSource>
         </environment>
     </environments>
      
    </configuration>

  7.创建employee的sql映射文件

1
2
3
4
5
6
7
8
9
<?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.pxxy.bean.mapper.EmployeeMapper">
    <select id="selectEmpById" resultType="com.pxxy.bean.Employee">
        select * from employee where id = #{id}
    </select>
</mapper>

  8.在mybatis的全局配置文件中添加映射

   9.创建测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MybatisTest1 {
     
    /**
     * 1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
         
        //获取sqlSession实例,能直接执行映射的sql语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
         
        Employee employee = sqlSession.selectOne("com.pxxy.bean.mapper.EmployeeMapper.selectEmpById", 1);
        System.out.println(employee);
         
        sqlSession.close();
    }<br>}

  10.运行test1()

二、接口式编程

  以上操作可不变动

  1.创建EmployeeMapper接口

 

   2.此时注意employee的sqlmapper中的命名空间是否是EmployeeMapper接口的全路径类名

  3.在测试类中添加方法2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 @Test
public void test2() throws IOException {
    String source = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(source);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
     
    //获取sqlSession实例,能直接执行映射的sql语句
    SqlSession sqlSession = sqlSessionFactory.openSession();
     
    EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
    System.out.println(employeeMapper.selectEmpById(1));
     
    sqlSession.close();
}

  4.运行结果

 

posted @   Arbitrary233  阅读(173)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示