- package com.ffyc.mybatisdemo.model;
-
- public class Admin {
-
- private int id;
- private String account;
- private String password;
- private String xb;
- private String adminPhone;
-
-
- public String getXb() {
- return xb;
- }
-
- public void setXb(String xb) {
- this.xb = xb;
- }
-
- public String getAdminPhone() {
- return adminPhone;
- }
-
- public void setAdminPhone(String adminPhone) {
- this.adminPhone = adminPhone;
- }
-
- public Admin() {
- System.out.println("Admin无参构造");
- }
-
- public Admin(String account, String password, String gender) {
- this.account = account;
- this.password = password;
- this.xb = gender;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- System.out.println("SetId");
- this.id = id;
- }
-
- public String getAccount() {
- return account;
- }
-
- public void setAccount(String account) {
- this.account = account;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
-
-
- @Override
- public String toString() {
- return "Admin{" +
- "id=" + id +
- ", account='" + account + '\'' +
- ", password='" + password + '\'' +
- ", xb='" + xb + '\'' +
- ", adminPhone='" + adminPhone + '\'' +
- '}';
- }
- }
注:
我们在创建实体类时尽量保证属性名与数据库中的字段名相同
在实体类中一定要给私有属性提供公共的get()和set()方法
保证类中有无参构造方法
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.4.2version>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.16version>
- dependency>
我们将此文件命名为config.properties
- driverName=com.mysql.cj.jdbc.Driver
- url=jdbc:mysql://127.0.0.1:3306/ssmdb?serverTimezone=Asia/Shanghai
- username=root
- password=111
注:
此处为键值对封装,所以配置属性文件中并没有双引号此处应特别注意
我们创建SQL映射文件后要在全局配置文件中进行注册,之后在此文件中进行SQL映射.
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.ffyc.mybatisdemo.dao.AdminDao">
- <select id="findAdminById" parameterType="int" resultType="Admin">
- select * from admin where id = #{id}
- select>
- mapper>
注:
全局配置文件用于配置数据库的连接信息,此处我们一次将后续经常使用的配置直接进行配置
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
-
-
- <properties resource="config.properties">properties>
-
- <settings>
-
- <setting name="logImpl" value="STDOUT_LOGGING"/>
-
- <setting name="mapUnderscoreToCamelCase" value="true"/>
-
- <setting name="cacheEnabled" value="true">setting>
- settings>
-
- <typeAliases>
-
- <package name="com.ffyc.mybatisdemo.model"/>
- typeAliases>
- <environments default="development">
- <environment id="development">
-
- <transactionManager type="JDBC"/>
-
- <dataSource type="POOLED">
- <property name="driver" value="${driverName}"/>
- <property name="url" value="${url}"/>
- <property name="username" value="${username}"/>
- <property name="password" value="${password}"/>
- dataSource>
- environment>
- environments>
-
-
- <mappers>
- <mapper resource="mappers/AdminMapper.xml"/>
- mappers>
- configuration>
注:
在此处我们需要在
必须在
开启日志功能之后Mybatis会自动帮我们打印日志信息
为我们的自定义类配置类型别名之后,我们在编写时就可以不用每次都编写全类名,直接写别名即可
我们需要将dao创建为一个接口,并在其中编写所需抽象方法
- package com.ffyc.mybatisdemo.dao;
-
- import com.ffyc.mybatisdemo.model.Admin;
- import org.apache.ibatis.annotations.Param;
-
- public interface AdminDao {
-
- Admin findAdminById(int id);
-
- }
- package com.ffyc.mybatisdemo.test;
-
- import com.ffyc.mybatisdemo.dao.AdminDao;
- import com.ffyc.mybatisdemo.model.Admin;
- import com.ffyc.mybatisdemo.util.MybatisUtil;
- import org.apache.ibatis.session.SqlSession;;
- import java.io.IOException;
-
- public class Test1 {
-
- public static void main(String[] args) throws IOException {
-
-
- //创建SqlSession
- SqlSession sqlSession = MybatisUtil.getSqlSession();
- // Admin admin = sqlSession.selectOne("com.ffyc.admin.findAdminById", 1);
-
- //动态生成一个代理对象(mybatis框架生成),由代理对象去调用mapper文件中的方法
- AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
- Admin admin = adminDao.findAdminById(1);
- System.out.println(admin);
- sqlSession.close();
- }
- }
注:
此处由于SqlSessionFactory创建的开销比较大,且是用于封装数据库的连接信息,所以我们没有必要在每个测试中都创建一次.因此我们直接将创建SQLSessionFactory的部分封装到一个Util类中,这样我们整个项目只有在第一次创建SQLSessionFactory,之后的使用过程中直接调用Util方法创建SQLSession即可.
- package com.ffyc.mybatisdemo.util;
-
- 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 MybatisUtil {
-
- static SqlSessionFactory sqlSessionFactory = null;
- static{
- //读取mybatis全局配置文件
- InputStream inputStream = null;
- try {
- inputStream = Resources.getResourceAsStream("mybatisConfig.xml");
- } catch (IOException e) {
- e.printStackTrace();
- }
- //创建SqlSessionFactory.由于SQLSessionFactory创建开销大,用于封装数据库连接信息,所以没有必要每次创建
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- }
-
- public static SqlSession getSqlSession(){
- return sqlSessionFactory.openSession();
- }
- }