企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 步骤 1. 创建maven工程并导入坐标. 2. 创建实体类和dao接口. 3. 创建MyBatis的主配置文件:SqlMapConfig.xml 4. 创建映射配置文件:IUserDao.xml. ## 环境搭建的注意事项 1. 创建IUuserDao.xml和IUserDao.java的名称是为了保持一致. 2. 在idea中创建目录的时候是和包是不一样的.包是可以自动带层级结构的,而目录在创建的时候是不带的. 3. mybatis的映射配置文件位置必须和dao接口的包结构相同. 4. 映射配置文件的mapper标签namespace属性的取值必须是dao接口的全限定类名. 5. 映射配置文件的操作配置,id属性的取值必须是dao接口的方法名. 6. 当我们遵循3,4,5点之后,我们在开发中就无序再写dao的实现类了. ![![](https://box.kancloud.cn/7c3f53e1f64752fa193337df82268b9f_886x942.png)](images/screenshot_1554120950615.png) ~~~ package com.like.dao; import com.like.domain.User; import java.util.List; public interface IUserDao { List<User> findAll(); } ~~~ ~~~ package com.like.daoimpl; import com.like.dao.IUserDao; import com.like.domain.User; import java.util.List; public class UserDaoImpl implements IUserDao { @Override public List<User> findAll() { return null; } } ~~~ ~~~ package com.like.domain; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private Integer id; private String username; private Date birthday; private String sex; private String address; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } ~~~ ~~~ <?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.like.dao.IUserDao"> <!--配置查询所有--> <select id="findAll" resultType="com.like.domain.User"> select * from user </select> </mapper> ~~~ ~~~ <?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"> <!--mybatis主配置文件--> <configuration> <!--配置环境--> <environments default="mysql"> <!--配置MySQL的环境--> <environment id="mysql"> <!--配置事务类型--> <transactionManager type="JDBC"/> <!--配置数据源(连接池)--> <dataSource type="POOLED"> <!--配置数据库的4个基本信息--> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.10.10:3306/jdbc?characterEncoding=utf8"/> <property name="username" value="homestead"/> <property name="password" value="secret"/> </dataSource> </environment> </environments> <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件--> <mappers> <mapper resource="com/like/dao/IUserDao.xml"/> </mappers> </configuration> ~~~ 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.like</groupId> <artifactId>mybatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatis</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project> ~~~ 测试: ~~~ @Test public void test() throws Exception { //读取配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); //创建SqlSessionFactory工厂 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream); //使用工厂生产SqlSession SqlSession session = build.openSession(); //使用SqlSession创建Dao接口的代理对象 IUserDao mapper = session.getMapper(IUserDao.class); //使用代理对象执行方法 List<User> users = mapper.findAll(); for (User user : users) { System.out.println(user); } //释放资源 session.close(); } ~~~ ## 使用注解开发 删除IUserDao.xml文件,其余的都是一样. ~~~ <mappers> <mapper class="com.like.dao.IUserDao"/> </mappers> ~~~ ~~~ public interface IUserDao { @Select("select * from user") List<User> findAll(); } ~~~ ## 执行过程 ![](https://box.kancloud.cn/d47235d5205ef16e47f4e6b45d04d721_2168x1028.png)