• Mybatis


    1. jdbc

       1 package com.gcl.jdbc;
       2 
       3 import java.sql.Connection;
       4 import java.sql.DriverManager;
       5 import java.sql.PreparedStatement;
       6 import java.sql.ResultSet;
       7 
       8 public class JDBCTest {
       9     public static void main(String[] args) throws Exception {
      10         String url="jdbc:mysql://localhost:3306/mybatis_study";
      11         String user="root";
      12         String password="root";
      13         String sql = "select * from user where username=?;";
      14         //加载驱动    
      15         Class.forName("com.mysql.jdbc.Driver");
      16         //获取连接
      17         Connection conn = DriverManager.getConnection(url, user, password);
      18         //获取预处理statement
      19         PreparedStatement pst = conn.prepareStatement(sql);
      20         //设置参数
      21         pst.setString(1, "张三丰");
      22         //获得查询结果集
      23         ResultSet ret = pst.executeQuery();
      24         //遍历结果集
      25         while(ret.next()){
      26             System.out.println(ret.getString("username"));
      27         }
      28         ret.close();
      29         pst.close();
      30         conn.close();
      31     }
      32 
      33 }
      View Code

      问题:开启关闭连接,造成数据库资源浪费,降低性能;SQL硬编码到代码中,不利于维护

    2. Mybatis入门程序
      添加jar包


      SqlMapConfig.xml是Mybatis核心配置文件,在里面配置数据源等,在里面还加入了编写有sql语句的User.xml文件
      配置log4j使sql语句打印到控制台:加入log4j和slf4j的包,然后在log4j.properties中

      1 # Global logging configuration
      2 #开发环境日志级别DEBUG 生产环境INFO或error
      3 log4j.rootLogger=DEBUG, stdout
      4 # Console output...
      5 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
      6 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
      7 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
      8 #log4j.logger.org.apache=INFO
      log4j.properties

      测试代码:

       1 package com.gcl.test;
       2 
       3 import java.io.InputStream;
       4 
       5 import org.apache.ibatis.io.Resources;
       6 import org.apache.ibatis.session.SqlSession;
       7 import org.apache.ibatis.session.SqlSessionFactory;
       8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
       9 import org.junit.Test;
      10 
      11 import com.gcl.po.User;
      12 
      13 public class TSET {
      14 
      15     @Test
      16     public void test() throws Exception {
      17         String resource = "mybatis/SqlMapConfig.xml";
      18         InputStream is = Resources.getResourceAsStream(resource);
      19         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
      20                 .build(is);
      21         SqlSession sqlsession = sqlSessionFactory.openSession();
      22         User user = sqlsession.selectOne("test.getUserbyid", 10);
      23         System.out.println(user.getAddress());
      24         sqlsession.close();
      25     }
      26 
      27 }
      View Code
  • 相关阅读:
    ABP 开发(一)
    转发: Angular装饰器
    AJAX上传文件到服务器
    转:system.Security.Cryptography C# 加密和解密
    asp.net mvc5 step by step(四)——关于Controller的ActionResult
    asp.net mvc5 step by step(三)—— Entity Framework Database First
    asp.net mvc5 step by step(二)——Data Annotations(data 注释)
    项目中使用protobuf
    架构私用Nuget服务器
    dockerfile语法
  • 原文地址:https://www.cnblogs.com/gucl/p/8263504.html
Copyright © 2020-2023  润新知