zoukankan      html  css  js  c++  java
  • 事务管理器 数据库连接属性

    mybatis-config.xml  配置文件  

    MyBatis Java API  Directory Structure  目录结构

    mybatis – MyBatis 3 | Java API http://www.mybatis.org/mybatis-3/java-api.html

    /my_application
      /bin
      /devlib
      /lib                <-- MyBatis *.jar files go here.
      /src
        /org/myapp/
          /action
          /data           <-- MyBatis artifacts go here, including, Mapper Classes, XML Configuration, XML Mapping Files.
            /mybatis-config.xml
            /BlogMapper.java
            /BlogMapper.xml
          /model
          /service
          /view
        /properties       <-- Properties included in your XML Configuration go here.
      /test
        /org/myapp/
          /action
          /data
          /model
          /service
          /view
        /properties
      /web
        /WEB-INF
          /web.xml


    在我们深入 Java API 之前,理解关于目录结构的最佳实践是很重要的。MyBatis 非常灵活,你可以用你自己的文件来做几乎所有的事情。但是对于任一框架,都有一些最佳的方式。

    让我们看一下典型的应用目录结构:  

    /my_application
      /bin
      /devlib
      /lib                <-- MyBatis *.jar 文件在这里。
      /src
        /org/myapp/
          /action
          /data           <-- MyBatis 配置文件在这里, 包括映射器类, XML 配置, XML 映射文件。
            /mybatis-config.xml
            /BlogMapper.java
            /BlogMapper.xml
          /model
          /service
          /view
        /properties       <-- 在你 XML 中配置的属性文件在这里。
      /test
        /org/myapp/
          /action
          /data
          /model
          /service
          /view
        /properties
      /web
        /WEB-INF
          /web.xml

    当然这是推荐的目录结构,并非强制要求,但是使用一个通用的目录结构将更利于大家沟通。

    这部分内容剩余的示例将假设你使用了这种目录结构。



    事务管理器  数据库连接属性 

    https://www.tutorialspoint.com/mybatis/mybatis_configuration_xml.htm

    transactionManager tag

    MyBatis supports two transaction managers namely JDBC and MANAGED

    • If we use the transaction manager of type JDBC, the application is responsible for the transaction management operations, such as, commit, roll-back, etc...

    • If we use the transaction manager of type MANAGED, the application server is responsible to manage the connection life cycle. It is generally used with the Web Applications.

    dataSource tag

    It is used to configure the connection properties of the database, such as driver-name, url, user-name, and password of the database that we want to connect. It is of three types namely −

    • UNPOOLED − For the dataSource type UNPOOLED, MyBatis simply opens and closes a connection for every database operation. It is a bit slower and generally used for the simple applications.

    • POOLED − For the dataSource type POOLED, MyBatis will maintain a database connection pool. And, for every database operation, MyBatis uses one of these connections, and returns them to the pool after the completion of the operation. It reduces the initial connection and authentication time that required to create a new connection.

    • JNDI − For the dataSource type JNDI, MyBatis will get the connection from the JNDI dataSource.

    写入数据库


    [root@d java]# java -jar /data/gateway/java/target/springMybatis-1.0-SNAPSHOT-jar-with-dependencies.jar
    record inserted successfully
    [root@d java]# java -jar /data/gateway/java/target/springMybatis-1.0-SNAPSHOT-jar-with-dependencies.jar
    record inserted successfully
    [root@d java]# tree -I target
    .
    ├── pom.xml
    └── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │   └── test
    │   │   ├── mybatisInsert.java
    │   │   └── Student.java
    │   └── resources
    │   ├── SqlMapConfig.xml
    │   └── StudentMapper.xml
    └── test
    └── java

    8 directories, 5 files
    [root@d java]# mvn clean; mvn compile;mvn package;

    com.test.Student

    package com.test;
    public class Student {
    private int id;
    private String name;
    private String branch;
    private int percentage;
    private int phone;
    private String email;

    public Student(String name, String branch, int percentage, int phone, String email) {
    super();
    this.name = name;
    this.branch = branch;
    this.percentage = percentage;
    this.phone = phone;
    this.email = email;
    }

    }

    com.test.mybatisInsert
    package com.test;

    import java.io.IOException;
    import java.io.Reader;

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;

    public class mybatisInsert {

    public static void main(String args[]) throws IOException {

    Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession session = sqlSessionFactory.openSession();

    //Create a new student object
    Student student = new Student("Mohammad", "It", 80, 984803322, "Mohammad@gmail.com");

    //Insert student data
    session.insert("Student.insert", student);
    System.out.println("record inserted successfully");
    session.commit();
    session.close();

    }

    }


    srcmain esourcesSqlMapConfig.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>
    <typeAliases>
    <typeAlias alias="Student" type="com.test.Student"/>
    </typeAliases>

    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://rmo.mysql.rds.aliyuncs.com:3306/video_test"/>
    <property name="username" value="t7"/>
    <property name="password" value="tI"/>
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <mapper resource="StudentMapper.xml"/>
    </mappers>
    </configuration>


    srcmain esourcesStudentMapper.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 = "Student">

    <insert id = "insert" parameterType = "Student">
    INSERT INTO STUDENT (NAME, BRANCH, PERCENTAGE, PHONE, EMAIL ) VALUES (#{name}, #{branch}, #{percentage}, #{phone}, #{email});

    <selectKey keyProperty = "id" resultType = "int" order = "AFTER">
    select last_insert_id() as id
    </selectKey>

    </insert>

    </mapper>

    pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>springMybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
    <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
    <manifest>
    <mainClass>com.test.mybatisInsert</mainClass>
    </manifest>
    </archive>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>single</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>

    <dependencies>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
    </dependency>


    </dependencies>

    </project>

    改进
    SqlSession session = sqlSessionFactory.openSession();
    try {
      BlogMapper mapper = session.getMapper(BlogMapper.class);
      // do work
    } finally {
      session.close();
    }











  • 相关阅读:
    第九十一天 how can I 坚持 技术-永远的技术
    第九十天 how can I 坚持
    Java控制台中输入中文输出乱码的解决办法
    【体系结构】转移预测器设计与比较1
    Ubuntu 13.04 用Sublime Text 2 编译运行 JAVA
    HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)
    个人重构机房收费系统之报表
    快速排序的递归和非递归实现
    HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest)
    体验决定深度,知识决定广度。你的人生是什么呢? 操蛋和扯蛋没必要纠结 唯有继续
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10069898.html
Copyright © 2011-2022 走看看