博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis SQL 增删改查代码实现
阅读量:5149 次
发布时间:2019-06-13

本文共 5684 字,大约阅读时间需要 18 分钟。

mybatis MySQL 增删改查代码实现

 

①:添加依赖

 

<!--导入依赖-->

<dependencies>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

 

</dependencies>

②:编写pojo对象

public class Domen {

private Integer uid;
private String username;
private String pwd;
private String sex;
private String address;

@Override
public String toString() {
return "Domen{" +
"uid=" + uid +
", username='" + username + '\'' +
", pwd='" + pwd + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}

public Integer getUid() {

return uid;
}

public void setUid(Integer uid) {

this.uid = uid;
}

public String getUsername() {

return username;
}

public void setUsername(String username) {

this.username = username;
}

public String getPwd() {

return pwd;
}

public void setPwd(String pwd) {

this.pwd = pwd;
}

public String getSex() {

return sex;
}

public void setSex(String sex) {

this.sex = sex;
}

public String getAddress() {

return address;
}

public void setAddress(String address) {

this.address = address;
}
}

 
   

 

③:  编写映射

<?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="djs">
<!--遍历全部-->
<select id="findAll" resultType="cn.mph.Domen">
select * from userpwd
</select>
<!--模糊查詢-->
<select id="findByUsername" parameterType="java.lang.String" resultType="cn.mph.Domen">
select * from userpwd where username like "%"#{username}"%"
</select>
<!--修改-->
<update id="update" parameterType="cn.mph.Domen">
UPDATE userpwd set username=#{username},pwd=#{pwd},sex=#{sex},address=#{address} where uid=#{uid};
</update>
<!--//删除-->
<delete id="delete" parameterType="java.lang.Integer" >
delete from userpwd where uid=#{uid}
</delete>
<!--添加数据-->
<insert id="insert" parameterType="cn.mph.Domen">
INSERT INTO userpwd values (null,#{username},#{pwd},#{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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"

//下面是对数据库的连接:127.0.0.1:3306这个本机数据库端口号,wed0000 表名

 

value="jdbc:mysql://127.0.0.1:3306/wed0000"/>

<property name="username" value="root"/>
<property name="password"

//密码

value="123456"/>

</dataSource>
</environment>
</environments>
<!--关联映射文件-->
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>

 

⑤:测试框架

package cn.mph;

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.InputStream;

import java.util.List;

public class TextCustomerFram {

@Test
//查询全部 遍历
public void text(){
//获取核心文件输入对象
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("SalMapConfig.xml");
//创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//创建SqlSessionFactory对象
SqlSessionFactory build = builder.build(stream);
//创建SqlSession对象 (SqlSessionFactory对象值.openSession())
SqlSession openSession = build.openSession();
//集合接收方便遍历
List<Object> list = openSession.selectList("djshd.findAll");
for (Object o:list) {
System.out.println(o);
}
openSession.close();
}
@Test
//条件查询
public void textFindByUsername(){
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("SalMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = builder.build(stream);
SqlSession session = build.openSession();
//
List<Domen> list = session.selectList("djshd.findByUsername", "P");
for (Domen o:list) {
System.out.println(o);
}
session.close();

}

@Test
//修改
public void textUpdate(){
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("SalMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = builder.build(stream);
SqlSession session = build.openSession();
Domen domen = new Domen();
domen.setUid(3);
domen.setUsername("武二");
domen.setPwd("250");
domen.setSex("男");
domen.setAddress("井冈山");
session.update("djs.update",domen);
session.commit();
session.close();
}
@Test
// 删除
public void textDelete(){
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("SalMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = builder.build(stream);
SqlSession session = build.openSession();
session.delete("djs.delete",3);
session.commit();
session.close();

}

@Test
// 添加
public void textInsert(){
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("SalMapConfig.xml");

SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

SqlSessionFactory build = builder.build(stream);

SqlSession session = build.openSession();

Domen domen = new Domen();

domen.setUsername("阿郎");
domen.setPwd("123");
domen.setSex("男");
domen.setAddress("N港");
session.insert("djs.insert",domen);
session.commit();
session.close();

}

}

 

转载于:https://www.cnblogs.com/mpenghao/p/11306823.html

你可能感兴趣的文章
鸡啄米vc++2010系列19(常用控件:静态文本框)
查看>>
【转】js中select的基本操作
查看>>
linux上的第一个c语言程序
查看>>
vmware实现物理机和虚拟机复制粘贴
查看>>
dom4j通过 xpath 处理xmlns
查看>>
spring集成redis
查看>>
网络抓包wireshark2
查看>>
搜搜反链暴增几十亿后,你还淡定吗?
查看>>
cocos2dx A*算法
查看>>
程序员常去的网站
查看>>
弹性盒 子
查看>>
情人节到了
查看>>
javaweb项目创建和虚拟主机配置
查看>>
安装jdk8-linux版
查看>>
sql树形结构 -函数统计节点极其子节点(Z)
查看>>
box-sizing
查看>>
【每天学点Python】案例七:模拟掷骰子
查看>>
Python进阶:生成器
查看>>
K大数查询(bzoj 3110)
查看>>
皇家马德里都在用的云服务,究竟是什么样的?
查看>>