项目概览
pom.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?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>maven5</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
测试类 1 2 3 4 5 6 7 8 9 10 public class DaoTest { @Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUser(201992001); System.out.println(user); } }
mybatis-config.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <?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> <properties> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://8.140.166.149:39002/mybatis" /> <property name="username" value="mybatis" /> <property name="password" value="jamay" /> </properties> <!-- 环境,可以配置多个,default:指定采用哪个环境 --> <environments default="test"> <!-- id:唯一标识 --> <environment id="test"> <!-- 事务管理器,JDBC类型的事务管理器 --> <transactionManager type="JDBC" /> <!-- 数据源,池类型的数据源 --> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <!-- 配置了properties,所以可以直接引用 --> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/MyMapper.xml" /> </mappers> </configuration>
可在 <properties resource="db.properties">
引入外部配置,加载时首先加载内部配置(<property name="url" value="jdbc:mysql://8.140.166.149:39002/mybatis" />
),然后加载外部配置(db.properties
),如果配置有重叠,会导致内部配置被外部配置覆盖,表现为外部配置优先级高
可设置别名(typeAliases)
MyMapper.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?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.zq.mapper.UserMapper"> <select id="getUser" resultType="com.zq.entity.User"> select * from student where id = #{id} </select> <select id="getUserList" resultType="com.zq.entity.User"> select * from student </select> <insert id="addUser"> insert into student values(#{id},#{name},#{passwd},${p_id}) </insert> <delete id="deleteUser"> delete from student where id= #{id} </delete> <update id="updateUser"> update student set p_id= #{p_id} where id= #{id} </update> </mapper>
MybatisUtils.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.zq.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { // 指定全局配置文件 String resource = "mybatis-config.xml"; // 读取配置文件 InputStream inputStream = Resources.getResourceAsStream(resource); // 构建sqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch (IOException e){ e.printStackTrace(); }} public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
UserMapper.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.zq.mapper; import com.zq.entity.User; import java.util.List; import java.util.Map; public interface UserMapper { List<User> getUserList(); User getUser(int id); int addUser(User user); int deleteUser(int id); int updateUser(Map<String, Object> map); }
User.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 package com.zq.entity; public class User { private String id; private String name; private String passwd; private String p_id; public User(String id, String name, String passwd, String p_id) { this.id = id; this.name = name; this.passwd = passwd; this.p_id = p_id; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getP_id() { return p_id; } public void setP_id(String p_id) { this.p_id = p_id; } @Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", password='" + passwd + '\'' + ", p_id='" + p_id + '\'' + '}'; } }