目录
基于Mybatis的实现--smbms
布图
Utils
MybatisUtils 工具类
package com.huan.utils;
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;
/*
* @Date 2021-07-25 14:48
* @Description MybatisUtils 工具类
* sqlSessionFactory 构建sqlSession
*/
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {//使用Mybatis第一步,获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession() {
//自动提交业务
return sqlSessionFactory.openSession(true);
}
}
resources下文件
-
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">
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<configuration>
<!--通过配置文件设置MySQL-->
<properties resource="db.properties">
</properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--给实体类起别名-->
<typeAliases>
<package name="com.huan.pojo"/>
<package name="com.huan.dao"/>
</typeAliases>
<!--数据库配置-->
<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>
<!--每一个mapper.xml都需要在mybatis核心配置文件中注册-->
<!--绑定接口类-->
<mappers>
<mapper resource="com/huan/dao/bill/BillMapper.xml"/>
<mapper resource="com/huan/dao/provider/ProviderMapper.xml"/>
<mapper resource="com/huan/dao/role/RoleMapper.xml"/>
<mapper resource="com/huan/dao/user/UserMapper.xml"/>
</mappers>
</configuration>
db.properties文件
数据库相关信息
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/smbms username =root password=root
实体类
pojo
Bill
package com.huan.pojo;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
/*
* @Date 2021-07-17 19:58
* @Description TODO
*/
@Data
public class Bill {
private Integer id;//id
private String billCode;//账单编码
private String productName;//商品名称
private String productDesc;//商品描述
private String productUnit;//商品单位
private BigDecimal productCount;//商品数量
private BigDecimal totalPrice;//总金额
private Integer isPayment;//是否支付
private Integer providerId;//供应商ID
private Integer createdBy;//创建者
private Date creationDate;//创建时间
private Integer modifyBy;//更新者
private Date modifyDate;//更新时间
private String providerName; //供应商名称
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductUnit() {
return productUnit;
}
public void setProductUnit(String productUnit) {
this.productUnit = productUnit;
}
public BigDecimal getProductCount() {
return productCount;
}
public void setProductCount(BigDecimal productCount) {
this.productCount = productCount;
}
public BigDecimal getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(BigDecimal totalPrice) {
this.totalPrice = totalPrice;
}
public Integer getIsPayment() {
return isPayment;
}
public void setIsPayment(Integer isPayment) {
this.isPayment = isPayment;
}
public Integer getProviderId() {
return providerId;
}
public void setProviderId(Integer providerId) {
this.providerId = providerId;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
}
Role
package com.huan.pojo;
import lombok.Data;
import java.util.Date;
/*
* @Date 2021-07-17 19:58
* @Description TODO
*/
@Data
public class Role {
private Integer id;//id
private String roleCode;//角色编码
private String roleName;//角色名称
private Integer createdBy;//创建者
private Date creationDate;//创建时间
private Integer modifyBy;//更新者
private Date modifyDate;//更新时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
Provider
package com.huan.pojo;
import lombok.Data;
import java.util.Date;
/*
* 作者: 王鹏欢
* @Date 2021-07-17 19:58
* @Description TODO
*/
@Data
public class Provider {
private Integer id;//id
private String proCode;//供应商编码
private String proName;//供应商名称
private String proDesc;//供应商描述
private String proContact;// 供应商联系人
private String proPhone;//供应商电话
private String proAddress;//供应商地址
private String proFax;//供应商传真
private Integer createdBy;//创建者
private Date creationDate;//创建时间
private Integer modifyBy;//更新者
private Date modifyDate;//更新时间
public Integer getId() {
return id;
}
public void setId(Integer 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 getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Provider() {
}
public Provider(Integer id, String proCode, String proName, String proDesc, String proPhone, String proContact, String proAddress, String proFax, Integer createdBy, Date creationDate, Date modifyDate, Integer modifyBy) {
this.id = id;
this.proCode = proCode;
this.proName = proName;
this.proDesc = proDesc;
this.proPhone = proPhone;
this.proContact = proContact;
this.proAddress = proAddress;
this.proFax = proFax;
this.createdBy = createdBy;
this.creationDate = creationDate;
this.modifyDate = modifyDate;
this.modifyBy = modifyBy;
}
}
User
package com.huan.pojo;
import java.util.Date;
/*
* 作者: 王鹏欢
* @Date 2021-07-17 19:57
* @Description TODO
*/
public class User {
private Integer id;//id
private String userCode;//用户编码
private String userName;//用户名称
private String userPassword;//用户密码
private Integer gender;//性别
private Date birthday;//出生日期
private String phone;//电话
private String address;//地址
private Integer userRole;//用户角色
private Integer createdBy;//创建者
private Date creationDate;//创建时间
private Integer modifyBy;//更新者
private Date modifyDate;//更新时间
private Integer age;
private String userRoleName; //用户角色名称
public Integer getAge() {
Date date = new Date();
return date.getYear() - birthday.getYear();
}
public void setAge(Integer age) {
this.age = age;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userCode='" + userCode + '\'' +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", gender=" + gender +
", birthday=" + birthday +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", userRole=" + userRole +
", createdBy=" + createdBy +
", creationDate=" + creationDate +
", modifyBy=" + modifyBy +
", modifyDate=" + modifyDate +
", age=" + age +
", userRoleName='" + userRoleName + '\'' +
'}';
}
}
dao
BillMapper
package com.huan.dao.bill;
import com.huan.pojo.Bill;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BillMapper {
//根据供应商ID查询订单数量
public int getBillCountByProviderId(@Param("providerId") Integer providerId) throws Exception;
//增加订单
public int add(Bill bill) throws Exception;
//通过查询条件获得供应商列表-getBillList
public List<Bill> getBillList(@Param("productName") String productName,
@Param("providerId") Integer providerId,
@Param("isPayment") Integer isPayment,
@Param("currentPagNo") Integer currentPagNo,
@Param("pageSize") Integer pageSize) throws Exception;
//通过条件查询-订单表记录数
public int getBillCount(@Param("productName") String productName,
@Param("providerId") Integer providerId,
@Param("isPayment") Integer isPayment) throws Exception;
//通过deleteId删除Bill
public int deleteBillById(@Param("id") Integer id) throws Exception;
//通过BillId获取Bill
public Bill getBillById(@Param("id") Integer id) throws Exception;
//修改订单信息
public int modify(Bill bill) throws Exception;
//根据供应商Id删除订单信息
public int deleteBillByProviderId(@Param("providerId") Integer providerId) throws Exception;
}
BillMapper.xml
<?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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.huan.dao.bill.BillMapper">
<!-- 增加订单-->
<insert id="add">
insert into smbms.smbms_bill (id,billCode, productName, productDesc, productUnit, productCount, totalPrice,
isPayment, createdBy,
creationDate, modifyBy, modifyDate, providerId)
values (#{id},#{billCode}, #{productName}, #{productDesc},
#{productUnit}, #{productCount}, #{totalPrice}, #{isPayment}, #{createdBy},
#{creationDate}, #{modifyBy}, #{modifyDate}, #{providerId});
</insert>
<!--修改订单信息-->
<update id="modify">
update smbms.smbms_bill
<set>
<if test="productDesc!=null">
productDesc=#{productDesc},
</if>
<if test="productName!=null">
productName = #{productName},
</if>
<if test="productCount!=null">
productCount=#{productCount},
</if>
</set>
where id = #{id};
</update>
<!--通过deleteId删除Bill-->
<delete id="deleteBillById">
delete
from smbms.smbms_bill
where id = #{id};
</delete>
<!--根据供应商Id删除订单信息-->
<delete id="deleteBillByProviderId">
delete
from smbms.smbms_bill
where providerId=#{providerId};
</delete>
<!--根据供应商ID查询订单数量-->
<select id="getBillCountByProviderId" resultType="java.lang.Integer">
select count(*)
from smbms.smbms_bill
where providerId = #{providerId}
</select>
<!--通过查询条件-模糊查询-获得供应商列表-getBillList-->
<select id="getBillList" resultType="com.huan.pojo.Bill">
select *
from smbms.smbms_bill
<where>
<if test="productName!=null">
and productName like concat ('%',#{productName},'%')
</if>
<if test="providerId!=null">
and providerId like concat ('%',#{providerId},'%')
</if>
<if test="isPayment!=null">
and isPayment like concat ('%',#{isPayment},'%')
</if>
</where>
limit #{currentPagNo},#{pageSize}
</select>
<!--通过条件查询-订单表记录数-->
<select id="getBillCount" resultType="java.lang.Integer">
select count(*)
from smbms.smbms_bill
<where>
<if test="productName!=null">
and productName=#{productName}
</if>
<if test="providerId!=null">
and providerId=#{providerId}
</if>
<if test="isPayment!=null">
and isPayment =#{isPayment}
</if>
</where>
</select>
<!--通过BillId获取Bill-->
<select id="getBillById" resultType="com.huan.pojo.Bill">
select *
from smbms.smbms_bill
<where>
id = #{id}
</where>
</select>
</mapper>
BillMapperTest
package com.huan.dao;
import com.huan.dao.bill.BillMapper;
import com.huan.pojo.Bill;
import com.huan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.List;
/*
* 作者: 2074785600
* @Date 2021-07-29 11:13
* @Description Bill的测试类
*/
public class BillMapperTest {
//根据供应商ID查询订单数量
@Test
public void getBillCountByProviderId() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
int i = mapper.getBillCountByProviderId(2);
System.out.println("一共有" + i + "条订单数量");
sqlSession.close();
}
//增加订单
@Test
public void add() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
Bill bill = new Bill();
bill.setProductName("李宁W72");
bill.setProductUnit("支");
bill.setProductDesc("运动器材");
bill.setIsPayment(1);
bill.setProviderId(1);
int i = mapper.add(bill);
if (i > 0) {
System.out.println("添加success");
}
sqlSession.close();
}
//通过查询条件获得供应商列表-getBillList
@Test
public void getBillList() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
List<Bill> list = mapper.getBillList("李", 1, 1,
0, 2);
for (Bill bill : list) {
System.out.println(bill);
}
sqlSession.close();
}
//通过条件查询-订单表记录数
@Test
public void getBillCount() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
int i = mapper.getBillCount("李宁W72", 1, 1);
System.out.println("一共有" + i + "条订单数量");
sqlSession.close();
}
//通过deleteId删除Bill
@Test
public void deleteBillById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
int i = mapper.deleteBillById(18);
if (i > 0) {
System.out.println("删除成功");
}
sqlSession.close();
}
//通过BillId获取Bill
@Test
public void getBillById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
Bill bill = mapper.getBillById(18);
System.out.println(bill);
sqlSession.close();
}
//修改订单信息
@Test
public void modify() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
Bill bill = new Bill();
bill.setProductName("Nike");
bill.setProductDesc("鞋");
bill.setId(19);
BigDecimal number = new BigDecimal(0);
int value = 499;
number = BigDecimal.valueOf((int) value);
bill.setProductCount(number);
mapper.modify(bill);
System.out.println();
sqlSession.close();
}
//根据供应商Id删除订单信息
@Test
public void deleteBillByProviderId() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
int i = mapper.deleteBillByProviderId(5);
if (i>0){
System.out.println("成功");
}
sqlSession.close();
}
}
ProviderMapper
package com.huan.dao.provider;
import com.huan.pojo.Provider;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ProviderMapper {
/**
* 增加供应商
*/
public int add(Provider provider) throws Exception;
/**
* 通过供应商名称、编码获取供应商列表-模糊查询-providerList
*/
public List<Provider> getProviderList(@Param("proName") String proName,
@Param("proCode") String proCode,
@Param("currentPagNo") Integer currentPagNo,
@Param("pageSize") Integer pageSize) throws Exception;
/**
* 获取供应商列表-模糊查询-proList
*/
public List<Provider> getProList() throws Exception;
/**
* 通过条件查询-获取供应商记录数
*/
public int getProviderCount(@Param("proName") String proName,
@Param("proCode") String proCode)throws Exception;
/**
* 通过proId删除Provider
*/
public int deleteProviderById(int id) throws Exception;
/**
* 通过proId获取Provider
*/
public Provider getProviderById(int id) throws Exception;
/**
* 修改用户信息
*/
public int modify(Provider provider) throws Exception;
}
ProviderMapper.xml
<?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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.huan.dao.provider.ProviderMapper">
<!-- 增加供应商-->
<insert id="add">
insert into smbms.smbms_provider (id, proCode, proName, proDesc, proContact, proPhone, proAddress, proFax,
createdBy, creationDate, modifyDate, modifyBy)
values (#{id}, #{proCode}, #{proName},
#{proDesc}, #{proContact}, #{proPhone}, #{proAddress}, #{proFax},
#{createdBy}, #{creationDate}, #{modifyDate}, #{modifyBy});
</insert>
<!--修改供应商信息-->
<update id="modify">
update smbms.smbms_provider
<set>
<if test="proName!=null">
proName=#{proName},
</if>
<if test="proAddress!=null">
proAddress = #{proAddress},
</if>
<if test="proCode!=null">
proCode = #{proCode},
</if>
</set>
where id = #{id};
</update>
<!--通过proId删除Provider-->
<delete id="deleteProviderById">
delete
from smbms.smbms_provider
where id = #{id};
</delete>
<!--通过供应商名称、编码获取供应商列表-模糊查询-providerList-->
<select id="getProviderList" resultType="com.huan.pojo.Provider">
select *
from smbms.smbms_provider
<where>
<if test="proName!=null">
and proName like concat ('%',#{proName},'%')
</if>
<if test="proCode!=null">
and proCode like concat ('%',#{proCode},'%')
</if>
</where>
limit #{currentPagNo},#{pageSize}
</select>
<!--获取供应商列表-proList-->
<select id="getProList" resultType="com.huan.pojo.Provider">
select *
from smbms.smbms_provider
</select>
<!--通过条件查询-获取供应商记录数-->
<select id="getProviderCount" resultType="java.lang.Integer">
select count(*)
from smbms.smbms_provider
<where>
<if test="proName!=null">
and proName=#{proName}
</if>
<if test="proCode!=null">
and proCode=#{proCode}
</if>
</where>
</select>
<!--通过proId获取Provider-->
<select id="getProviderById" resultType="com.huan.pojo.Provider">
select *
from smbms.smbms_provider
<where>
id = #{id}
</where>
</select>
</mapper>
ProviderMapperTest
package com.huan.provider;
import com.huan.dao.provider.ProviderMapper;
import com.huan.pojo.Provider;
import com.huan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
/*
* 作者: 2074785600
* @Date 2021-07-29 11:13
* @Description Bill的测试类
*/
public class ProviderMapperTest {
//通过条件查询-获取供应商记录数
@Test
public void getProviderCount() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
int i = mapper.getProviderCount("陕西省西安市", "SX_GYS001");
System.out.println("一共有" + i + "条订单数量");
sqlSession.close();
}
// 增加供应商
@Test
public void add() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
Provider provider = new Provider();
provider.setProContact("小米");
provider.setProCode("SX_GYS001");
provider.setProName("陕西省西安市");
provider.setProPhone("2074785600");
int i = mapper.add(provider);
if (i > 0) {
System.out.println("添加success");
}
sqlSession.close();
}
//通过供应商名称、编码获取供应商列表-模糊查询-providerList
@Test
public void getProviderList() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
mapper.getProviderList("陕西", "001", 0, 2);
sqlSession.close();
}
//通过供应商供应商列表-providerList
@Test
public void getProList() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
List<Provider> proList = mapper.getProList();
for (Provider provider : proList) {
System.out.println(provider);
}
sqlSession.close();
}
//通过proId获取Provider
@Test
public void getProviderById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
Provider provider = mapper.getProviderById(14);
System.out.println(provider);
System.out.println();
sqlSession.close();
}
//修改供应商信息
@Test
public void modify() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
Provider provider = new Provider();
provider.setProName("陕西省西安市");
provider.setProCode("SX_GYS001");
provider.setId(14);
mapper.modify(provider);
System.out.println();
sqlSession.close();
}
//通过proId删除Provider
@Test
public void deleteProviderById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
int i = mapper.deleteProviderById(14);
if (i > 0) {
System.out.println("成功");
}
sqlSession.close();
}
}
RoleMapper
package com.huan.dao.role;
import com.huan.pojo.Role;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface RoleMapper {
//获取角色列表
public List<Role> getRoleList()throws Exception;
//增加角色信息
public int add(Role role)throws Exception;
//通过Id删除Role
public int deleteRoleById(@Param("id") String Id)throws Exception;
//修改角色信息
public int modify(Role role)throws Exception;
//通过Id获取role
public Role getRoleById(@Param("id") Integer id)throws Exception;
//根据roleCode,进行角色编码的唯一性验证(统计count)
public int roleCodeIsExist(@Param("roleCode") String roleCode)throws Exception;
}
RoleMapper.xml
<?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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.huan.dao.role.RoleMapper">
<!-- 增加角色信息-->
<insert id="add">
insert into smbms.smbms_role (id, roleCode, roleName, createdBy, creationDate, modifyBy, modifyDate)
values (#{id}, #{roleCode}, #{roleName},
#{createdBy}, #{creationDate}, #{modifyBy}, #{modifyDate});
</insert>
<!--修改角色信息-->
<update id="modify">
update smbms.smbms_role
<set>
<if test="roleName!=null">
roleName = #{roleName},
</if>
<if test="roleCode!=null">
roleCode=#{roleCode},
</if>
</set>
where id = #{id};
</update>
<!--通过Id删除Role-->
<delete id="deleteRoleById">
delete
from smbms.smbms_role
where id = #{id};
</delete>
<!--获取角色列表-->
<select id="getRoleList" resultType="com.huan.pojo.Role">
select *
from smbms.smbms_role
</select>
<!--通过Id获取role-->
<select id="getRoleById" resultType="com.huan.pojo.Role">
select *
from smbms.smbms_role
where id = #{id}
</select>
<!--根据roleCode,进行角色编码的唯一性验证(统计count)-->
<select id="roleCodeIsExist" resultType="java.lang.Integer">
select count(*)
from smbms.smbms_role
where roleCode = #{roleCode}
</select>
</mapper>
RoleMapperTest
package com.huan.role;
import com.huan.dao.role.RoleMapper;
import com.huan.pojo.Role;
import com.huan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
/*
* 作者: 2074785600
* @Date 2021-07-29 11:13
* @Description Bill的测试类
*/
public class RoleMapperTest {
//根据roleCode,进行角色编码的唯一性验证(统计count)
@Test
public void roleCodeIsExist() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
int i = mapper.roleCodeIsExist("SMBMS_ADMIN");
System.out.println("一共有" + i + "条订单数量");
sqlSession.close();
}
//增加角色信息
@Test
public void add() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
Role role = new Role();
role.setId(4);
role.setRoleName("洛尘");
int i = mapper.add(role);
if (i > 0) {
System.out.println("添加success");
}
sqlSession.close();
}
//获取角色列表
@Test
public void getRoleList() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
List<Role> roleList = mapper.getRoleList();
for (Role role : roleList) {
System.out.println(role);
}
sqlSession.close();
}
//通过Id删除Role
@Test
public void deleteRoleById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
int i = mapper.deleteRoleById("4");
if (i > 0) {
System.out.println("删除成功");
}
sqlSession.close();
}
//通过Id获取role
@Test
public void getRoleById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
Role roleById = mapper.getRoleById(1);
System.out.println(roleById);
sqlSession.close();
}
//修改角色信息
@Test
public void modify() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
Role role = new Role();
role.setId(2);
role.setRoleName("总裁");
int i = mapper.modify(role);
if (i > 0) {
System.out.println("成功");
}
System.out.println();
sqlSession.close();
}
}
UserMapper
package com.huan.dao.user;
import com.huan.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
//通过userCode获取User
public User getLoginUser(@Param("userCode") String userCode) throws Exception;
//增加用户信息
public int add(User user) throws Exception;
//通过条件查询userList,limit分页
public List<User> getUserList(@Param("userName") String userName,
@Param("userRole") Integer userRole,
@Param("currentPageNO") Integer currentPageNO,
@Param("pageSize") Integer pageSize) throws Exception;
//通过条件查询-用户记录数
public int getUserCount(@Param("userName") String userName,
@Param("userRole") Integer userRole) throws Exception;
//通过userId删除user
public int deleteUserById(@Param("id") Integer id);
//通过useId获取user
public User getUserById(@Param("id") Integer id);
//修改用户信息
public int modify(User user);
//修改当前用户密码
public int updatePwd(@Param("id") Integer id, @Param("userPassword") String userPassword);
}
UserMapper.xml
<?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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.huan.dao.user.UserMapper">
<!-- 增加用户信息-->
<insert id="add">
insert into smbms.smbms_user (id, userCode, userName, userPassword, gender, birthday, phone, address, userRole,
createdBy, creationDate, modifyBy, modifyDate)
values (#{id}, #{userCode}, #{userName},
#{userPassword}, #{gender}, #{birthday}, #{phone}, #{address},
#{userRole}, #{createdBy}, #{creationDate}, #{modifyBy}, #{modifyDate});
</insert>
<!--修改用户信息-->
<update id="modify">
update smbms.smbms_user
<set>
<if test="userName!=null">
userName=#{userName},
</if>
<if test="userPassword!=null">
userPassword = #{userPassword},
</if>
<if test="gender!=null">
gender=#{gender},
</if>
<if test="phone!=null">
phone=#{phone},
</if>
<if test="userRole!=null">
userRole=#{userRole},
</if>
<if test="userCode!=null">
userCode=#{userCode},
</if>
</set>
where id = #{id};
</update>
<!--修改当前用户密码-->
<update id="updatePwd">
update smbms.smbms_user
<set>
<if test="userPassword!=null">
userPassword = #{userPassword},
</if>
</set>
where id = #{id};
</update>
<!--通过userId删除user-->
<delete id="deleteUserById">
delete
from smbms.smbms_user
where id = #{id};
</delete>
<!--通过userCode获取User-->
<select id="getLoginUser" resultType="com.huan.pojo.User">
select *
from smbms.smbms_user
where userCode = #{userCode}
</select>
<!--通过条件查询userList,limit分页-->
<select id="getUserList" resultType="com.huan.pojo.User">
select *
from smbms.smbms_user
<where>
<if test="userName!=null">
and userName like concat ('%',#{userName},'%')
</if>
<if test="userRole!=null">
and userRole like concat ('%',#{userRole},'%')
</if>
</where>
limit #{currentPageNO},#{pageSize}
</select>
<!--通过条件查询-用户记录数-->
<select id="getUserCount" resultType="java.lang.Integer">
select count(*)
from smbms.smbms_user
<where>
<if test="userRole!=null">
and userRole=#{userRole}
</if>
<if test="userName!=null">
and userName=#{userName}
</if>
</where>
</select>
<!--通过useId获取user-->
<select id="getUserById" resultType="com.huan.pojo.User">
select *
from smbms.smbms_user
<where>
id = #{id}
</where>
</select>
</mapper>
UserMapperTest
package com.huan.user;
import com.huan.dao.user.UserMapper;
import com.huan.pojo.User;
import com.huan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
/*
* 作者: 2074785600
* @Date 2021-07-29 11:13
* @Description Bill的测试类
*/
public class UserMapperTest {
//通过userCode获取User
@Test
public void getLoginUser() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getLoginUser("admin");
System.out.println(user);
sqlSession.close();
}
//增加用户信息
@Test
public void add() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(13);
user.setUserCode("luochen");
user.setUserName("洛尘");
user.setUserPassword("74108520");
user.setUserRole(1);
int i = mapper.add(user);
if (i > 0) {
System.out.println("添加success");
}
sqlSession.close();
}
//通过条件查询userList,limit分页
@Test
public void getUserList() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList("洛", 1,
0, 2);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
//通过条件查询-用户记录数
@Test
public void getUserCount() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.getUserCount("洛尘", 1);
System.out.println("一共有" + i + "条订单数量");
sqlSession.close();
}
//通过userId删除user
@Test
public void deleteUserById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUserById(13);
if (i > 0) {
System.out.println("删除成功");
}
sqlSession.close();
}
//通过useId获取user
@Test
public void getUserById() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(13);
System.out.println(user);
sqlSession.close();
}
//修改用户信息
@Test
public void modify() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUserPassword("123123");
user.setPhone("123456");
user.setId(13);
mapper.modify(user);
System.out.println();
sqlSession.close();
}
//修改当前用户密码
@Test
public void updatePwd() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.updatePwd(13, "1111");
if (i > 0) {
System.out.println("成功");
}
sqlSession.close();
}
}