类结构:
第一步:创建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>
<properties resource="jdbc.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- //映射文件-->
<mappers>
<mapper resource="com/dao/ProviderMapper.xml"/>
</mappers>
</configuration>
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///smbms
username=root
password=123456
第二步:编写实体类Provider.java
package com.bean;
import java.util.Date;
/**
* @auther zll
* @create 2020/7/8-18:56
*/
public class Provider {
private int id;
private String proCode;
private String proName;
private String proDesc;
private String proContact;
private String proPhone;
private String proFax;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
private String companyLicPicPath;
private String orgCodePicPath;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public String getCompanyLicPicPath() {
return companyLicPicPath;
}
public void setCompanyLicPicPath(String companyLicPicPath) {
this.companyLicPicPath = companyLicPicPath;
}
public String getOrgCodePicPath() {
return orgCodePicPath;
}
public void setOrgCodePicPath(String orgCodePicPath) {
this.orgCodePicPath = orgCodePicPath;
}
@Override
public String toString() {
return "Provider{" +
"id=" + id +
", proCode='" + proCode + '\'' +
", proName='" + proName + '\'' +
", proDesc='" + proDesc + '\'' +
", proContact='" + proContact + '\'' +
", proPhone='" + proPhone + '\'' +
", proFax='" + proFax + '\'' +
", createdBy=" + createdBy +
", creationDate=" + creationDate +
", modifyBy=" + modifyBy +
", modifyDate=" + modifyDate +
", companyLicPicPath='" + companyLicPicPath + '\'' +
", orgCodePicPath='" + orgCodePicPath + '\'' +
'}';
}
}
第三步:编写接口ProviderMapper 和 SQL映射文件ProviderMapper.xml
package com.dao;
import java.util.List;
/**
* @auther zll
* @create 2020/7/8-19:05
*/
public interface ProviderMapper {
public int count();
public List<?> getProviderAll();
}
<?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.ProviderMapper">
<select id="count" resultType="Integer">
select count(1) from smbms_provider
</select>
<select id="getProviderAll" resultType="com.bean.Provider">
select * from smbms_provider;
</select>
</mapper>
第四步:测试类TestProviderMapper.java
package com.test;
import com.bean.Provider;
import com.util.SqlSessionUtil;
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.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @auther zll
* @create 2020/7/8-19:15
*/
public class TestProviderMapper {
@Test
public void test(){
String str = "mybatis-config.xml";
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream(str);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
int count = sqlSession.selectOne("com.dao.ProviderMapper.count");
System.out.println("用户个数:"+count);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(sqlSession != null ){
sqlSession.close();
}
}
}
@Test
public void test1(){
String str = "mybatis-config.xml";
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream(str);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
List<Provider> list = sqlSession.selectList("com.dao.ProviderMapper.getProviderAll");
for(Provider p : list){
System.out.println(p.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(sqlSession != null ){
sqlSession.close();
}
}
}
@Test
public void test2(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
List<Provider> list = sqlSession.selectList("com.dao.ProviderMapper.getProviderAll");
for(Provider p : list){
System.out.println(p.toString());
}
SqlSessionUtil.closeSqlsession(sqlSession);
}
}
第五步:编写SqlSessionUtil 工具类
package com.util;
import com.bean.Provider;
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;
import java.util.List;
/**
* @auther zll
* @create 2020/7/8-19:31
*/
public class SqlSessionUtil {
public static SqlSession getSqlSession(){
String str = "mybatis-config.xml";
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream(str);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
List<Provider> list = sqlSession.selectList("com.dao.ProviderMapper.getProviderAll");
for(Provider p : list){
System.out.println(p.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
public static void closeSqlsession(SqlSession sqlSession){
if(sqlSession != null){
sqlSession.close();
}
}
}