标签:cto rom target catch project actor 传统 使用 url
MyBatis
是一款优秀的持久层框架 ,它支持定制化 SQL
、存储过程
以及高级映射
。
MyBatis
避免了几乎所有的 JDBC 代码
和手动设置参数
以及获取结果集
。
MyBatis
可以使用简单的 XML
或注解
来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
Mybatis中文文档:
https://mybatis.org/mybatis-3/zh/index.html
Maven仓库:https://mvnrepository.com/
Maven仓库
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
帮助程序员将数据存入到数据库中、方便。
传统的JDBC代码太复杂了。简化、框架、自动化。
优点:
因为使用的是MySql
可视化工具,所以我描述我创建了什么
创建了,数据库mybatis
,表user
,表中内容id
、name
、pwd
之后,在其中随便加入一些数据。
<?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>org.example</groupId>
<artifactId>Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
也可以加入下面这个配置
功能:主要用于打包资源文件
,默认
情况下maven
只打src/main/resource
下的资源。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
<!--账号-->
<property name="username" value="root"/>
<!--密码-->
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--两种方式 Dao类文件-->
<mappers>
<mapper resource="Dao/UserDao.xml"/>
</mappers>
</configuration>
public class mybatis_util {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
//return sqlSessionFactory.openSession(true);
//第二种是自动提交事物 增删改就不用写sqlSession.commit();
}
}
public class user {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", pwd=‘" + pwd + ‘\‘‘ +
‘}‘;
}
public user() {
}
public user(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
}
public interface UserDao {
public List<user> getUserList();
}
<?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">
<!--namespace中是Dao中接口类位置-->
<mapper namespace="com.Dao.UserDao">
<select id="getUserList" resultType="pojo.user">
select * from mybatis.user
</select>
</mapper>
上述两个文件,放在一个包下,必须命名相同
。
public class Test {
@org.junit.Test
public void test(){
//第一步:获得SqlSession对象
SqlSession sqlSession = mybatis_util.getSqlSession();
//执行SQL
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<user> userList = mapper.getUserList();
for (user user : userList) {
System.out.println(user);
}
//关闭SqlSession
sqlSession.close();
}
}
个人博客为:
MoYu‘s Github Blog
MoYu‘s Gitee Blog
标签:cto rom target catch project actor 传统 使用 url
原文地址:https://www.cnblogs.com/MoYu-zc/p/14406813.html