事务管理器 数据库连接属性
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();
}
}
src\main\resources\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>
<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>
src\main\resources\StudentMapper.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();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2017-12-05 crontab -e 定时任务中的 脚本文件 路径
2017-12-05 无限次重启该脚本
2017-12-05 公司 怎么提问
2017-12-05 武汉哪里有卖篮球架的 n-gram
2017-12-05 eval
2017-12-05 接口(Interfaces)与反射(reflection) 如何利用字符串驱动不同的事件 动态地导入函数、模块
2017-12-05 compile java sources