Addition deletion and revision of mybatis
- 2021-10-27 07:38:58
- OfStack
Directory jar package needs 3 connection to database and jar package of mybatis. Create database below, copy it to document 1, and import it into database. mybatisUtils file mybatis. xml configuration file ParkingMapp interface ParkingMapper. xml file for addition, deletion and modification. Check sql sentence model class ParkingInfo. Finally, test class: summary
The jar package requires 3 jar packages that connect to the database and mybatis
Create the following database, copy it to the 1 document and import it into the database
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50731
Source Host : localhost:3306
Source Database : ssm_dome_tingche01
Target Server Type : MYSQL
Target Server Version : 50731
File Encoding : 65001
Date: 2021-07-12 16:49:15
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tbl_parking
-- ----------------------------
DROP TABLE IF EXISTS `tbl_parking`;
CREATE TABLE `tbl_parking` (
`pId` int(11) NOT NULL AUTO_INCREMENT,
`carNo` varchar(50) DEFAULT NULL,
`pMark` varchar(20) DEFAULT NULL,
PRIMARY KEY (`pId`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tbl_parking
-- ----------------------------
INSERT INTO `tbl_parking` VALUES ('1', ' Zhejiang A88888', '123456');
INSERT INTO `tbl_parking` VALUES ('2', ' Beijing A88888', '123456');
INSERT INTO `tbl_parking` VALUES ('3', ' Yu A88888', '123456');
INSERT INTO `tbl_parking` VALUES ('4', ' In fact, in fact, the A88888', '124566');
INSERT INTO `tbl_parking` VALUES ('5', ' Wan A88888', '123456');
-- ----------------------------
-- Table structure for tbl_parking_detail
-- ----------------------------
DROP TABLE IF EXISTS `tbl_parking_detail`;
CREATE TABLE `tbl_parking_detail` (
`pdId` int(11) NOT NULL AUTO_INCREMENT,
`pId` int(11) DEFAULT NULL,
`beginDate` datetime NOT NULL,
`endDate` datetime DEFAULT NULL,
`pDur` int(11) DEFAULT NULL,
`pCost` float DEFAULT NULL,
`pName` varchar(11) DEFAULT NULL,
PRIMARY KEY (`pdId`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tbl_parking_detail
-- ----------------------------
INSERT INTO `tbl_parking_detail` VALUES ('2', '2', '2019-05-15 17:06:30', '2019-05-15 19:06:30', '2', '30', ' In fact, in fact, the 4');
INSERT INTO `tbl_parking_detail` VALUES ('3', '3', '2019-05-15 17:06:30', '2019-05-15 20:06:30', '1', '50', ' Li 4');
INSERT INTO `tbl_parking_detail` VALUES ('4', '4', '2021-07-12 10:57:25', '2021-07-14 14:17:52', '48', '200', ' In fact, in fact, the 4');
INSERT INTO `tbl_parking_detail` VALUES ('5', '5', '2021-07-22 14:17:37', '2021-07-23 14:17:40', '1', '5', ' In fact, in fact, the ');
INSERT INTO `tbl_parking_detail` VALUES ('6', '1', '2019-05-15 17:06:30', '2019-05-15 18:06:30', '1', '11', 'dd');
mybatisUtils file
package cn.hp.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
private static SqlSessionFactory sessionFactory;
static{
String resource="mybatis.xml";
try {
InputStream is=Resources.getResourceAsStream(resource);
sessionFactory=new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSession(){
return sessionFactory.openSession();
}
public static void closeSession(SqlSession session){
if(session!=null){
session.close();
}
}
}
mybatis. xml configuration file
<?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>
<package name="cn.hp.model"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm_dome_tingche01?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/hp/dao/ParkingMapper.xml"></mapper>
</mappers>
</configuration>
ParkingMapp interface
package cn.hp.dao;
import cn.hp.model.ParkingInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface ParkingMapper {
public List<ParkingInfo> findAll();
// Cha Da Yu Da Yu 30 Adj.
public List<ParkingInfo> findPCost(int pCost);
// Check a stop information
public ParkingInfo findById(String id);
// Fuzzy search of root principal name
public List<ParkingInfo> findParkName(String name);
// Cha Da Yu Da Yu 30 Adj. And the fuzzy search of the main name
public List<ParkingInfo> findPark(@Param("pCost")float pCost,@Param("pName")String pName);
//Map Accept
public List<ParkingInfo> findParkByMap(Map<String, Object> map);
// Divide certain information
public int deletePark(int pdid);
// Modify some information
public int update(ParkingInfo pi);
// Add 1 In fact, in fact, the
public int add(ParkingInfo pi);
}
ParkingMapper. xml file for addition, deletion and modification, check sql sentence
<?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="cn.hp.dao.ParkingMapper">
<resultMap id="parkingList" type="cn.hp.model.ParkingInfo">
<result property="pdid" column="pdid"></result>
<result property="pid" column="pid"></result>
<result property="beginDate" column="beginDate"></result>
<result property="endDate" column="endDate"></result>
<result property="pDur" column="pDur"></result>
<result property="pCost" column="pCost"></result>
<result property="pName" column="pName"></result>
<result property="carNo" column="carNo"></result>
</resultMap>
<select id="findAll" resultMap="parkingList">
select d.*,p.carNo from tbl_parking_detail d,tbl_parking p where d.pid=p.pid
</select>
<select id="findPCost" parameterType="int" resultType="cn.hp.model.ParkingInfo">
select * from tbl_parking_detail where pCost>#{pCost}
</select>
<select id="findById" parameterType="String" resultType="cn.hp.model.ParkingInfo">
select * from tbl_parking_detail where pdid=#{pdid}
</select>
<select id="findParkName" resultType="cn.hp.model.ParkingInfo">
select * from tbl_parking_detail where pName like '%${pName}%'
</select>
<select id="findPark" resultType="cn.hp.model.ParkingInfo">
select * from tbl_parking_detail where pName like '%${pName}%' and pCost>#{pCost}
</select>
<select id="findParkByMap" parameterType="map" resultType="cn.hp.model.ParkingInfo">
select * from tbl_parking_detail where pDur=#{pDur} and pCost>#{pCost}
</select>
<delete id="deletePark" parameterType="cn.hp.model.ParkingInfo" >
delete from tbl_parking_detail where pdid=#{pdid}
</delete>
<update id="update" parameterType="cn.hp.model.ParkingInfo">
update tbl_parking_detail set pName =#{pName} where pdid=#{pdid}
</update>
<insert id="add" parameterType="cn.hp.model.ParkingInfo">
insert into tbl_parking_detail values (null,#{pid},#{beginDate},#{endDate},#{pDur},#{pCost},#{pName})
</insert>
</mapper>
model class ParkingInfo
package cn.hp.model;
public class ParkingInfo {
public int pdid;
public int pid;
public String beginDate;
public String endDate;
public int pDur;
public String pName;
public float pCost;
public String carNo;
public int getPdid() {
return pdid;
}
public void setPdid(int pdid) {
this.pdid = pdid;
}
public ParkingInfo(int pid, String beginDate, String endDate, int pDur, String pName, float pCost, String carNo) {
this.pid = pid;
this.beginDate = beginDate;
this.endDate = endDate;
this.pDur = pDur;
this.pName = pName;
this.pCost = pCost;
this.carNo = carNo;
}
@Override
public String toString() {
return "ParkingInfo{" +
"pdid=" + pdid +
", pid=" + pid +
", beginDate='" + beginDate + '\'' +
", endDate='" + endDate + '\'' +
", pDur=" + pDur +
", pName='" + pName + '\'' +
", pCost=" + pCost +
", carNo='" + carNo + '\'' +
'}';
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getBeginDate() {
return beginDate;
}
public void setBeginDate(String beginDate) {
this.beginDate = beginDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public int getpDur() {
return pDur;
}
public void setpDur(int pDur) {
this.pDur = pDur;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public float getpCost() {
return pCost;
}
public void setpCost(float pCost) {
this.pCost = pCost;
}
public String getCarNo() {
return carNo;
}
public void setCarNo(String carNo) {
this.carNo = carNo;
}
public ParkingInfo(int pdid, int pid, String beginDate, String endDate, int pDur, String pName, float pCost, String carNo) {
this.pdid = pdid;
this.pid = pid;
this.beginDate = beginDate;
this.endDate = endDate;
this.pDur = pDur;
this.pName = pName;
this.pCost = pCost;
this.carNo = carNo;
}
public ParkingInfo() {
}
}
Finally, there is the test class:
package cn.hp.test;
import cn.hp.dao.ParkingMapper;
import cn.hp.model.ParkingInfo;
import cn.hp.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test01 {
public static void main(String[] args) {
// test1();
// test2();
// test3();
// test4();
// test5();
// test6();
// test7();
// test8();
test9();
}
private static void test9() {
SqlSession session =MybatisUtils.getSession();
int i= session.getMapper(ParkingMapper.class).add(new ParkingInfo(1,"2019-05-15-17:06:30","2019-05-15-18:06:30",1,"dd",11,"sss"));
if (i>0){
System.out.println(" Modify ok");
}
session.commit();
}
private static void test8(){
SqlSession session =MybatisUtils.getSession();
ParkingInfo pi= session.getMapper(ParkingMapper.class).findById("2");
pi.setpName(" In fact, in fact, the 4");
int result=session.getMapper(ParkingMapper.class).update(pi);
if (result>0){
System.out.println(" Modify ok");
}
session.commit();
}
private static void test7() {
SqlSession session =MybatisUtils.getSession();
int result= session.getMapper(ParkingMapper.class).deletePark(1);
if (result>0){
System.out.println(" Division ok");
}
session.commit();
}
private static void test6() {
SqlSession session =MybatisUtils.getSession();
Map<String, Object> map = new HashMap<String, Object>();
map.put("pDur",1);
map.put("pCost",30);
List<ParkingInfo> parkingInfoList= session.getMapper(ParkingMapper.class).findParkByMap(map);
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getpCost()+"\t"+s.getpName());
}
}
private static void test5() {
SqlSession session =MybatisUtils.getSession();
List<ParkingInfo> parkingInfoList= session.getMapper(ParkingMapper.class).findPark(30," In fact, in fact, the ");
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getpCost()+"\t"+s.getpName());
}
}
private static void test4() {
SqlSession session =MybatisUtils.getSession();
List<ParkingInfo> parkingInfoList= session.getMapper(ParkingMapper.class).findParkName("4");
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getPdid()+"\t"+s.getpName());
}
}
private static void test3() {
SqlSession session =MybatisUtils.getSession();
ParkingInfo si= session.getMapper(ParkingMapper.class).findById("2");
System.out.println(si.getPdid()+"\t"+si.getPid()+"\t"+si.beginDate+"\t"+si.endDate+"\t"+si.pDur+"\t"+si.pCost);
}
private static void test2() {
SqlSession session =MybatisUtils.getSession();
List<ParkingInfo> parkingInfoList= session.getMapper(ParkingMapper.class).findPCost(30);
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getPdid()+"\t"+s.getpCost());
}
}
private static void test1() {
SqlSession session =MybatisUtils.getSession();
List<ParkingInfo> parkingInfoList= session.getMapper(ParkingMapper.class).findAll();
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getPdid()+"\t"+s.getCarNo());
}
}
}
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!