首先往pom文件中加入配置以获取jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
编写一个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>
<!--配置环境-->
<environments default="development">
<environment id="development">
<!--jdbc事务-->
<transactionManager type="JDBC"/>
<!--连接池-->
<dataSource type="POOLED">
<!--驱动-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--数据库链接-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8"/>
<!--用户名-->
<property name="username" value="root"/>
<!--密码-->
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--映射文件-->
<mapper resource="com/hw/entity/Student.xml"/>
</mappers>
</configuration>
创建实体类和实体类的配置文件
package com.hw.entity;
/**
* @program: Maven
* @description:
* @author: hw
* @create: 2019-01-01 00:42
**/
public class Student {
private Integer sid;
private String sname;
private Integer tid;
public Student() {
super();
}
public Student(String sname, Integer tid) {
this.sname = sname;
this.tid = tid;
}
public Student(Integer sid, String sname, Integer tid) {
this.sid = sid;
this.sname = sname;
this.tid = tid;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", tid=" + tid +
'}';
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
}
<?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="student">
<!--根据id查-->
<!-- id statement的id 唯一
parameterType 传入类型
resultType 单个查询返回结果类型
#{} 一个占位符
#{id} 该占位符等待接收参数的名称为Id
-->
<select id="findStudentById" resultType="com.hw.entity.Student" parameterType="int">
select * from student where sid=#{id}
</select>
<!--根据名字模糊查询-->
<!--
${} 表示拼接sql字符串
${value} 表示拼接的是简单类型参数
1.如果参数为简单类型时,${}里面的参数名称必须为value 固定写法
2.${}为引起sql注入,一般不推荐使用,但某些情况下必须使用,如order by ${name}
-->
<select id="findStudentByName" parameterType="String" resultType="com.hw.entity.Student">
select * from student where sname like '%${value}%'
</select>
<!--添加-->
<!--占位符对应对象属性-->
<insert id="addStudent" parameterType="com.hw.entity.Student">
insert into student (sname,tid)
values (#{sname},#{tid});
</insert>
<!--删除-->
<delete id="delStudent" parameterType="int">
delete from student where sid =#{id}
</delete>
<!--修改-->
<update id="editStudent" parameterType="com.hw.entity.Student">
update student set sname=#{sname},tid=#{tid} where sid =#{sid};
</update>
</mapper>
测试:
package com.hw.test;
import com.hw.entity.Student;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @program: Maven
* @description:
* @author: hw
* @create: 2019-01-01 01:12
**/
public class Demo1 {
InputStream inputStream;
SqlSessionFactory build;
SqlSession sqlSession;
@Before
public void before() {
try {
//读取配置文件
inputStream = Resources.getResourceAsStream("Mybatis.xml");
//通过sqlsessionFactoryBuilder创建sqlsessionFactory会话工厂
build = new SqlSessionFactoryBuilder().build(inputStream);
//通过sqlsessionFactory创建sqlsession
sqlSession = build.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void after() {
//关闭sqlsession
sqlSession.commit();
sqlSession.close();
}
/**
* @Description: 查询
* @Param: []
* @return: void
* @Author: hw
* @Date: 2019/1/1
*/
@Test
public void test1() throws IOException {
//调用sqlsession的操作数据库的方法
// Student student=sqlSession.selectOne("findStudentById",1);
List<Student> findStudentByName = sqlSession.selectList("findStudentByName", "1");
findStudentByName.forEach(x -> System.out.println(x));
//关闭sqlsession
sqlSession.close();
}
/**
* @Description: 添加
* @Param: []
* @return: void
* @Author: hw
* @Date: 2019/1/1
*/
@Test
public void test2() {
Student student = new Student("猴子", 1);
int addStudent = sqlSession.insert("addStudent", student);
System.out.println(addStudent);
}
/**
* @Description: 删除
* @Param: []
* @return: void
* @Author: hw
* @Date: 2019/1/1
*/
@Test
public void test3() {
System.out.println(sqlSession.delete("delStudent", 2));
}
/**
* @Description: 修改
* @Param:
* @return:
* @Author: hw
* @Date: 2019/1/1
*/
@Test
public void test4(){
Student student=new Student(1,"李箐",2);
System.out.println(sqlSession.update("editStudent",student));
}
}
还没有评论,来说两句吧...