参考网址:https://mybatis.org/mybatis-3/zh/getting-started.html 接上一个JDBC数据库操作之后,然后使用Mybatis进行数据库操作。 https://github.com/mybatis/mybatis-3/releases
配置文件 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"> <parent> <artifactId>mybatissimple01</artifactId> <groupId>com.ycxy</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mybatis01</artifactId> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> </dependencies> </project>resources mybatis-config.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> <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://192.168.172.130:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="Websites.xml"/> </mappers> </configuration>Mapper文件Websites.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="com.ycxy.WebsitesMapper"> <select id="selectOne" resultType="com.ycxy.Websites"> select * from websites where id = #{id} </select> </mapper>Websites.java
package com.ycxy; public class Websites { private int id; private String name ; private String url; private int alexa; private String country; 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 getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getAlexa() { return alexa; } public void setAlexa(int alexa) { this.alexa = alexa; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return "id:"+id + " name: "+ name + " url: " + url; } }WebsitesMapper.java
package com.ycxy; import java.util.List; public interface WebsitesMapper { public Websites selectOne(int id); }id:1 name: Google url: https://www.google.cm/
