之后给项目命名即可
dao 文件夹用于写对应sql语句的接口
domain 用于写对应数据库表属性的DataBean类
mapper 文件夹用于存写sql语句的xml文件
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<?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.jdbc.Driver" />
<!-- 指定数据库-->
<property name="dataset" value="jdbc:mysql://localhost:3306/javaee_exp?characterEncoding=utf-8"/>
<!-- 指定用户-->
<property name="user" value="root"/>
<!-- 用户密码-->
<property name="pwd" value="pwd"/>
</properties>
<!-- 配置开发环境 -->
<environments default="develop">
<environment id="develop">
<!-- 1.1指定事务管理 将事物交给JDBC来管理 -->
<transactionManager type="JDBC"/>
<!-- 1.2datasource(数据源/连接池,)指定链接数据库的基本信息 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${dataset}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pwd}"/>
</dataSource>
</environment>
</environments>
</configuration>
package com.domain;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String firstName;
private String lastName;
private int salary;
public User() {}
public User(int id, String firstName, String lastName, int salary) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName=‘" + firstName + ‘\‘‘ +
", lastName=‘" + lastName + ‘\‘‘ +
", salary=" + salary +
‘}‘;
}
}
package com.dao;
import com.domain.User;
import java.util.List;
public interface UserDao2 {
List<User> findAll();
User getUserById(int id);
}
<?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.dao.UserDao2">
<!-- select是查询标签,用来提供查询语句的支持 -->
<!-- id绑定接口内的实现方法名 -->
<!-- resultType指定返回值的数据类型,如果返回值是一个集合,则填入该集合的泛型对象 -->
<select id="findAll" resultType="com.domain.User">
SELECT * FROM demo1;
</select>
<!-- 使用设置的别名-->
<select id="getUserById" parameterType="int" resultType="User">
SELECT * FROM demo1 WHERE id=#{id};
</select>
<!-- 以下为其他方式, 不对应这次的数据库-->
<!-- JAVA类型-->
<!-- <select id="selectUserByName" parameterType="java.lang.String"-->
<!-- resultType="User">-->
<!-- select * from user where account like ‘%${value}%‘-->
<!-- </select>-->
<!-- <insert id="insertUser3" parameterType="com.isoftstone.mybatis.pojo.User">-->
<!-- keyProperty:返回的主键存储在javaBean中的哪个属性-->
<!-- resultTyoe:返回的主键是什么类型-->
<!-- order:在数据插入之前还是插入之后返回-->
<!-- –>-->
<!-- <selectKey keyProperty="id" resultType="java.lang.String" order="BEFORE" >-->
<!-- select sys_guid() from dual-->
<!-- </selectKey>-->
<!-- insert into user-->
<!-- (id,username,birthday,sex,address)-->
<!-- values-->
<!-- (#{id},#{username},#{birthday},#{sex},#{address})-->
<!-- </insert>-->
</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">
<configuration >
<!-- 设置属性 -->
<properties>
<!-- 指定连接器, 一般不用修改-->
<property name="driver" value="com.mysql.jdbc.Driver" />
<!-- 指定数据库-->
<property name="dataset" value="jdbc:mysql://localhost:3306/javaee_exp?characterEncoding=utf-8"/>
<!-- 指定用户-->
<property name="user" value="root"/>
<!-- 用户密码-->
<property name="pwd" value="pwd"/>
</properties>
<!-- 新增, 可不用-->
<!-- 配置别名-->
<typeAliases>
<typeAlias type="com.domain.User" alias="User"/>
<package name="com.domain"/>
</typeAliases>
<!-- 配置开发环境 -->
<environments default="develop">
<environment id="develop">
<!-- 1.1指定事务管理 将事物交给JDBC来管理 -->
<transactionManager type="JDBC"/>
<!-- 1.2datasource(数据源/连接池,)指定链接数据库的基本信息 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${dataset}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pwd}"/>
</dataSource>
</environment>
</environments>
<!-- 新增, 用于映射-->
<!-- 导入mapper文件(sql语句) -->
<mappers>
<!-- 两种方法-->
<!-- resource文件的xml文件-->
<mapper resource="mapper/UserDao2.xml"/>
<!-- 类, 主要适用于使用注解写sql语句的情况-->
<mapper class="com.dao.UserDao"/>
</mappers>
</configuration>
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.dao.UserDao2;
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.Before;
import org.junit.Test;
import com.dao.UserDao;
import com.domain.User;
public class MybitsTest1 {
private SqlSession session = null;
@Before /*注解标记的方法会在@Test注解标记的方法之前执行*/
public void testBefore() throws IOException {
//读取mybatis核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
// 获取SqlSessionFactory工厂对象
SqlSessionFactory fac =
new SqlSessionFactoryBuilder().build(in);
// 获取sqlsesssion对象
session = fac.openSession(true);//true表示自动提交事物,false表示手动提交事务
}
/*接口开发*/
@Test
public void testFindAll01() throws IOException {
// 注解测试
// UserDao mapper = session.getMapper(UserDao.class);
// List<User> list = mapper.findAll();
// for (User emp : list) {
// System.out.println(emp.toString());
// }
// mapper.insertValue(5, "dd", "gg", 300);
// xml测试
UserDao2 mapper = session.getMapper(UserDao2.class);
List<User> list = mapper.findAll();
for (User emp : list) {
System.out.println(emp.toString());
}
System.out.print("-->");
System.out.println(mapper.getUserById(4).toString());
}
}
原文:https://www.cnblogs.com/ag-chen/p/13961394.html