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();

}


}