写点什么

Mybatis 学习笔记 --Mybatis 的概念与入门案例,java 中高级面试题最新

用户头像
极客good
关注
发布于: 刚刚

address varchar(256) default null comment '地址',


primary key (id)


) engine=InnoDB DEFAULT CHARSET =utf8;



新建 maven 工程:



目录结构:



在 pom.xml 中导入相关依赖 jar 包:


<?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_day01</artifactId>


<version>1.0-SNAPSHOT</version>


<packaging>jar</packaging>


<dependencies>


<dependency>


<groupId>org.mybatis</groupId>


<artifactId>mybatis</artifactId>


<version>3.4.5</version>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


<version>8.0.11</version>


</dependency>


<dependency>


<groupId>log4j</groupId>


<artifactId>log4j</artifactId>


<version>1.2.12</version>


</dependency>


<dependency>


<groupId>junit</groupId>


<artifactId>junit</artifactId>


<version>4.10</version>


</dependency>


</dependencies>


</project>


配置封装数据库数据的 User 类:


public class User implements Serializable {


private Integer id;


private String username;


private Date birthday;


private String sex;


private String 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;}


@Override


public String toS


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


tring() {


return "User{" +


"id=" + id +


", username='" + username + ''' +


", birthday=" + birthday +


", sex='" + sex + ''' +


", address='" + address + ''' +


'}';


}


}


配置持久层接口 IUserDao:


/**


  • 用户持久层接口


*/


public interface IUserDao {


/**


  • 查询所有

  • @return


*/


List<User> findAll();


}


配置接口映射文件 IUserDao.xml:


<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE mapper


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"


"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.IUserDao">


<select id="findAll" resultType="com.User">


select * from user


</select>


</mapper>


配置 SqlMapConfig.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>


<properties>


<property name="driver" value="com.mysql.cj.jdbc.Driver"></property>


<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8"></property>


<property name="username" value="root"></property>


<property name="password" value="123456"></property>


</properties>


<environments default="mysql">


<environment id="mysql">


<transactionManager type="JDBC"/>


<dataSource type="POOLED">


<property name="driver" value="${driver}"></property>


<property name="url" value="${url}"></property>


<property name="username" value="${username}"></property>


<property name="password" value="${password}"></property>


</dataSource>


</environment>


</environments>


<mappers>


<mapper resource="com/IUserDao.xml"/>


</mappers>


</configuration>

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
Mybatis学习笔记--Mybatis的概念与入门案例,java中高级面试题最新