Mybatis、作业:实现企业名称、地址二级搜索
一、Mybatis
mybatis流程图:
mybatis配置文件:
<?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>
<typeAliases>
<typeAlias alias="User" type="com.yiibai.mybatis.models.User" />
</typeAliases>
<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://127.0.0.1:3306/yiibai" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/yiibai/mybatis/models/User.xml" />
</mappers>
</configuration>
实体类User.java
package com.yiibai.mybatis.models;
public class User {
private int id;
private String name;
private String dept;
private String phone;
private String website;
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
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 getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
test.java
import java.io.Reader;
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 com.yiibai.mybatis.models.*;
public class Test {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("config/Configure.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SqlSession session = sqlSessionFactory.openSession();
try {
User user = (User) session.selectOne(
"com.yiibai.mybatis.models.UserMapper.GetUserByID", 1);
if(user!=null){
String userInfo = "名字:"+user.getName()+", 所属部门:"+user.getDept()+", 主页:"+user.getWebsite();
System.out.println(userInfo);
}
} finally {
session.close();
}
}
}
User.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命名空间,做sql隔离 -->
<mapper namespace="com.yiibai.mybatis.models.UserMapper">
<!--
id:sql语句唯一标识
parameterType:指定传入参数类型(对应javaBean类型,写原始型会自动包装为包装类)
resultType:返回结果类型
#{}:占位符号,起到占位作用,如果传入的是原始型,那么括号中的变量名称可以随意定义
-->
<select id="GetUserByID" parameterType="int" resultType="User">
select * from `user` where id = #{id}
</select>
</mapper>
二、作业:实现企业名称、地址二级搜索
添加以下文件:
1.Enterprise实体类
2.EnterpriseController
3.EnterpriseService
4.EnterpriseServiceImpl
5.EnterpriseMapper
6.EnterpriseMapper.xml
7.前端Enterprise.html
代码如下
1.Enterprise实体类
package com.stylefeng.guns.modular.webSys.model;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
/**
* <p>
* 企业表
* </p>
*
* @author yfq
* @since 2019-07-23
*/
@TableName("bw_enterprise")
public class Enterprise extends Model<Enterprise> {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@TableId(value="id", type= IdType.AUTO)
private Integer id;
/**
* 企业名称
*/
@TableField("e_business_name")
private String eBusinessName;
/**
* 电话
*/
@TableField("e_telephone")
private String eTelephone;
/**
* 官网
*/
@TableField("e_websiteurl")
private String eWebsiteurl;
/**
* 邮箱
*/
@TableField("e_mailbox")
private String eMailbox;
/**
* 地址
*/
@TableField("e_address")
private String eAddress;
/**
* 简介
*/
@TableField("e_introduction")
private String eIntroduction;
/**
* 创建时间
*/
@TableField("e_creation_time")
@DateTimeFormat(pattern = "yyyy-mm-dd HH:MM:SS")
private Date eCreationTime;
/**
* 创建用户
*/
@TableField("e_creating_users")
private Integer eCreatingUsers;
/**
* 修改时间
*/
@TableField("e_edit_time")
@DateTimeFormat(pattern = "yyyy-mm-dd HH:MM:SS")
private Date eEditTime;
/**
* 修改用户
*/
@TableField("e_edit_users")
private Integer eEditUsers;
/**
* 发布时间
*/
@TableField("e_release_time")
@DateTimeFormat(pattern = "yyyy-mm-dd HH:MM:SS")
private Date eReleaseTime;
/**
* 发布用户
*/
@TableField("e_release_users")
private Integer eReleaseUsers;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String geteBusinessName() {
return eBusinessName;
}
public void seteBusinessName(String eBusinessName) {
this.eBusinessName = eBusinessName;
}
public String geteTelephone() {
return eTelephone;
}
public void seteTelephone(String eTelephone) {
this.eTelephone = eTelephone;
}
public String geteWebsiteurl() {
return eWebsiteurl;
}
public void seteWebsiteurl(String eWebsiteurl) {
this.eWebsiteurl = eWebsiteurl;
}
public String geteMailbox() {
return eMailbox;
}
public void seteMailbox(String eMailbox) {
this.eMailbox = eMailbox;
}
public String geteAddress() {
return eAddress;
}
public void seteAddress(String eAddress) {
this.eAddress = eAddress;
}
public String geteIntroduction() {
return eIntroduction;
}
public void seteIntroduction(String eIntroduction) {
this.eIntroduction = eIntroduction;
}
public Date geteCreationTime() {
return eCreationTime;
}
public void seteCreationTime(Date eCreationTime) {
this.eCreationTime = eCreationTime;
}
public Integer geteCreatingUsers() {
return eCreatingUsers;
}
public void seteCreatingUsers(Integer eCreatingUsers) {
this.eCreatingUsers = eCreatingUsers;
}
public Date geteEditTime() {
return eEditTime;
}
public void seteEditTime(Date eEditTime) {
this.eEditTime = eEditTime;
}
public Integer geteEditUsers() {
return eEditUsers;
}
public void seteEditUsers(Integer eEditUsers) {
this.eEditUsers = eEditUsers;
}
public Date geteReleaseTime() {
return eReleaseTime;
}
public void seteReleaseTime(Date eReleaseTime) {
this.eReleaseTime = eReleaseTime;
}
public Integer geteReleaseUsers() {
return eReleaseUsers;
}
public void seteReleaseUsers(Integer eReleaseUsers) {
this.eReleaseUsers = eReleaseUsers;
}
@Override
protected Serializable pkVal() {
return this.id;
}
@Override
public String toString() {
return "Enterprise{" +
"id=" + id +
", eBusinessName=" + eBusinessName +
", eTelephone=" + eTelephone +
", eWebsiteurl=" + eWebsiteurl +
", eMailbox=" + eMailbox +
", eAddress=" + eAddress +
", eIntroduction=" + eIntroduction +
", eCreationTime=" + eCreationTime +
", eCreatingUsers=" + eCreatingUsers +
", eEditTime=" + eEditTime +
", eEditUsers=" + eEditUsers +
", eReleaseTime=" + eReleaseTime +
", eReleaseUsers=" + eReleaseUsers +
"}";
}
}
2.EnterpriseController
package com.stylefeng.guns.modular.webSys.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.webSys.model.Enterprise;
import com.stylefeng.guns.modular.webSys.service.IEnterpriseService;
import com.stylefeng.guns.modular.webSys.warpper.EnterpriseWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
/**
* 企业信息控制器
*
* @author yfq
* @Date 2019年7月23日
*/
@Controller
@RequestMapping("/enterprise")
public class EnterpriseController extends BaseController {
private String PREFIX = "/webSys/Enterprise/";
@Autowired
private IEnterpriseService enterpriseService;
/**
* 跳转到查看企业信息页面
*/
@RequestMapping("")
public String index() {
return PREFIX + "Enterprise.html";
}
/**
* 获取所有部门列表
*/
@RequestMapping(value = "/list")
@ResponseBody
public Object list(String condition,String addCondition) {
//方法1:
// EntityWrapper<Enterprise> enterpriseEntityWrapper = new EntityWrapper<>();
// if(ToolUtil.isNotEmpty(condition) || ToolUtil.isNotEmpty(addCondition)){
// enterpriseEntityWrapper.like("e_business_name",condition);
// enterpriseEntityWrapper.like("e_address",addCondition);
// }
// List<Map<String, Object>> list = this.enterpriseService.selectMaps(enterpriseEntityWrapper);
// return super.warpObject(new EnterpriseWarpper(list));
//方法2
List<Map<String, Object>> list = this.enterpriseService.list(condition,addCondition);
return super.warpObject(new EnterpriseWarpper(list));
}
}
3.EnterpriseService
package com.stylefeng.guns.modular.webSys.service;
import com.baomidou.mybatisplus.service.IService;
import com.stylefeng.guns.modular.webSys.model.Enterprise;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* 企业表
*
* @author yfq
* @date 2019-07-23
*/
public interface IEnterpriseService extends IService<Enterprise> {
/**
* 获取所有企业信息列表
*/
List<Map<String, Object>> list(@Param("condition") String condition ,@Param("addCondition") String addCondition);
}
4.EnterpriseServiceImpl
package com.stylefeng.guns.modular.webSys.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.webSys.dao.EnterpriseMapper;
import com.stylefeng.guns.modular.webSys.model.Enterprise;
import com.stylefeng.guns.modular.webSys.service.IEnterpriseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {
@Override
public List<Map<String, Object>> list(String condition,String addCondition) {
return this.baseMapper.list(condition ,addCondition);
}
}
5.EnterpriseMapper
package com.stylefeng.guns.modular.webSys.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.stylefeng.guns.modular.webSys.model.Enterprise;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* <p>
* 企业表 Mapper 接口
* </p>
*
* @author yfq
* @since 2019-07-23
*/
public interface EnterpriseMapper extends BaseMapper<Enterprise> {
/**
* 获取所有企业信息列表
*/
List<Map<String, Object>> list(@Param("condition") String condition,@Param("addCondition") String addCondition);
}
6.EnterpriseMapper.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">
<mapper namespace="com.stylefeng.guns.modular.webSys.dao.EnterpriseMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.stylefeng.guns.modular.webSys.model.Enterprise">
<id column="id" property="id"/>
<result column="e_business_name" property="eBusinessName"/>
<result column="e_telephone" property="eTelephone"/>
<result column="e_websiteurl" property="eWebsiteurl"/>
<result column="e_mailbox" property="eMailbox"/>
<result column="e_address" property="eAddress"/>
<result column="e_introduction" property="eIntroduction"/>
<result column="e_creation_time" property="eCreationTime"/>
<result column="e_creating_users" property="eCreatingUsers"/>
<result column="e_edit_time" property="eEditTime"/>
<result column="e_edit_users" property="eEditUsers"/>
<result column="e_release_time" property="eReleaseTime"/>
<result column="e_release_users" property="eReleaseUsers"/>
</resultMap>
<!--通用查询结果列-->
<sql id="Base_Column_List">
id,
e_business_name as eBusinessName,
e_telephone as eTelephone,
e_websiteurl as eWebsiteurl,
e_mailbox as eMailbox,
e_address as eAddress,
e_introduction as eIntroduction,
e_creation_time as eCreationTime,
e_creating_users as eCreatingUsers,
e_edit_time as eEditTime,
e_edit_users as eEditUsers,
e_release_time as eReleaseTime,
e_release_users as eReleaseUsers
</sql>
<select id="list" resultType="map">
select <include refid="Base_Column_List"/> from bw_enterprise
<if test="(condition != null and condition != '')or(addCondition != null and addCondition != '') ">
where e_business_name like CONCAT('%',#{condition},'%') and e_address like CONCAT('%',#{addCondition},'%')
</if>
order by e_release_time DESC
</select>
</mapper>
7.前端Enterprise.html
@layout("/common/_container.html"){
<div class="row">
<div class="col-sm-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>企业信息管理</h5>
</div>
<div class="ibox-content">
<div class="row row-lg">
<div class="col-sm-12">
<div class="row">
<div class="col-sm-3">
<#NameCon id="condition" name="名称" />
</div>
<div class="col-sm-3">
<select id="addCondition" style="height:34px;">
<option></option>
<option>浙江省杭州市滨江区网商路699号</option>
<option>河北省秦皇岛市海港区</option>
<option>美国洛杉矶</option>
<option>英国伦敦</option>
<option>法国巴黎</option>
</select>
</div>
<div class="col-sm-3">
<#button name="搜索" icon="fa-search" clickFun="Enterprise.search()"/>
</div>
</div>
<div class="hidden-xs" id="EnterpriseTableToolbar" role="group">
@if(shiro.hasPermission("/dept/add")){
<#button name="添加" icon="fa-plus" clickFun="Dept.openAddDept()"/>
@}
@if(shiro.hasPermission("/dept/update")){
<#button name="修改" icon="fa-plus" clickFun="Dept.openDeptDetail()" space="true"/>
@}
@if(shiro.hasPermission("/dept/delete")){
<#button name="删除" icon="fa-plus" clickFun="Dept.delete()" space="true"/>
@}
</div>
<#table id="EnterpriseTable"/>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="${ctxPath}/static/modular/webSys/Enterprise/Enterprise.js"></script>
@}
数据库如下所示:
运行结果:
在这里插入图片描述