1、搭建数据库
- 这里使用sqlyog来配合mysql搭建数据库,首先打开命令窗口,输入mysql -uroot -p登录mysql,输入mysql密码之后,然后按下回车键,出现welcome的字样表示登陆成功。
- 创建名为mybatis的数据库,并创建名为user的表
- 往user表中插入数据
2、创建mybatis项目
- 使用IDEA先创建一个普通的maven项目,并删除src
- 在pom.xml中导入依赖以及配置resources(pom.xml)
<!--导入依赖-->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version> 4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 在项目中创建子maven项目,且在子项目的resoures文件中创建mybatis的核心配置文件,名为mybatis-config.xml
并在核心配置文件中进行环境配置(其中的url,username,password,resource需要根据自己的设置进行调整)(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核心配置文件-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/cat/dao/UserMappr.xml"/>
</mappers>
</configuration>
编写mybatis工具类 (MybatisUtils)
//配置解析过程
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//0
String resource = "mybatis-config.xml";
//1.读取resources下面的mybatis-config.xml文件
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.使用SqlSessionFactoryBuilder创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSession的实例,用以执行已映射的Sql语句
public static SqlSession getsqlSession() {
//3.通过sqlSessionFactory创建SqlSession
return sqlSessionFactory.openSession();
}
}
编写数据库实体类(User)
根据数据库中表的属性进行编写
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
编写接口对应的配置文件(UserMapper.xml)
其中的方法对应着想要对数据库进行的操作
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户
User getUserById(int id);
User getUserById2(Map<String,Object> map);
//insert一个用户
int addUser(User user);
//万能map
int addUser2(Map<String,Object> map);
//修改用户
int updateUser(User user);
//删除一个用户
int deleteUser(int id);
}
编写测试类(UserTest)
@Test
public void test(){
//第一步获得sqlsession对象
SqlSession sqlSession = MybatisUtils.getsqlSession();
//方式一:getmapper
//执行sql
UserMapper userDao =sqlSession.getMapper(UserMapper.class);//get完接口,就会返回这个接口的类型
List<User> userList =userDao.getUserList();//就可以执行接口中的方法,实际通过sqlSession去执行xml中的sql语句
for(User user : userList){
System.out.println(user);
}
sqlSession.close();//关闭sqlSession
}