直接选择next下一步
选择Finish
package com.sunyan.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private String sex;
private Date birthday;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
'}';
}
}
复制头部约束
<?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>
<!--配置环境-->
<environments default="mysql">
<!--配置mysql的环境-->
<environment id="mysql">
<!--配置事务-->
<transactionManager type="JDBC"></transactionManager>
<!--配置连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--配置映射文件的位置-->
<mappers>
<mapper resource="com/sunyan/dao/UserDao.xml"></mapper>
</mappers>
</configuration>
在com.sunyan.dao中创建接口UserDao
package com.sunyan.dao;
import com.sunyan.domain.User;
import java.util.List;
/**
* 持久层接口
*/
public interface UserDao {
/**
* 查询所有用户
* @return
*/
List<User> findAll();
}
在resource目录下创建文件
注意:对照包的结构,一级一级地创建文件夹
复制配置文件头部约束,并添加配置
<?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.sunyan.dao.UserDao">
<!--查询所有-->
<select id="findAll" resultType="com.sunyan.domain.User">
select * from user;
</select>
</mapper>
namesqpce:确定该方法在哪一个dao接口
id:哪一个方法
resultType:要封装到哪里去
创建
编写代码
package com.sunyan.test;
import com.sunyan.dao.UserDao;
import com.sunyan.domain.User;
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 org.junit.Test;
import java.io.InputStream;
import java.util.List;
/**
* 测试mybatis的crud操作
*/
public class MybatisTest {
/**
* 测试查询所有
*/
@Test
public void testFindAll() throws Exception {
//1.读取配置文件,生成字节输入流
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.获取SqlSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//3.获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//4.获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//5.执行查询所有方法
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
//6.释放资源
sqlSession.close();
in.close();
}
}
原文:https://www.cnblogs.com/sunyanblog/p/12380581.html