• 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.创建数据源文件

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

      6.创建mybatis全局配置文件

    <?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映射文件

    <?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.创建测试类

    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();
    	}
    }

      10.运行test1()

    二、接口式编程

      以上操作可不变动

      1.创建EmployeeMapper接口

     

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

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

         @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.运行结果

  • 相关阅读:
    [Swift实际操作]七、常见概念-(5)使用NSString对字符串进行各种操作
    [Swift]LeetCode326. 3的幂 | Power of Three
    [Swift]LeetCode303. 区域和检索
    [Swift]LeetCode292. Nim游戏 | Nim Game
    [Swift]LeetCode290. 单词模式 | Word Pattern
    SpringMVC框架中的异常解析器-ExceptionHandler和HandlerExceptionResolver
    一个Web报表项目的性能分析和优化实践(七):性能监测工具JavaMelody
    一个Web报表项目的性能分析和优化实践(七):性能监测工具JavaMelody
    百度Echarts-免费的商业产品图表库
    百度Echarts-免费的商业产品图表库
  • 原文地址:https://www.cnblogs.com/lyh233/p/12336040.html
Copyright © 2020-2023  润新知