1.首先建立学生表:
2.需要用到的相关包:
3.mybatis.xml配置:
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<typeAliases>
<package name="com.po"/>
</typeAliases>
<!-- 定义环境-->
<environments default="mysql">
<environment id="mysql">
<!-- 定义事务-->
<transactionManager type="JDBC"></transactionManager>
<!--定义数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="wu"/>
</dataSource>
</environment>
</environments>
<mappers >
<mapper resource="com/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
4.DButil里面的相关内容:
package com.util;
import java.io.Reader;
import java.util.List;
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 DButil {
public static SqlSessionFactory factory;
static {
//通过字符流的读取将主配置文件读取到程序中;
try {
Reader reader= Resources.getResourceAsReader("mybatis.xml");
//创建会话工厂建造者对象
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
//得到会话工厂对象
factory=builder.build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
//得到链接对象
public static SqlSession getSession(boolean auto){
return factory.openSession(auto);
}
}
5.com.po包里面对学生信息进行封装;
package com.po;
public class Student {
private int id;
private String username;
private String sex;
private String age;
private String banji;
private String hobby;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getBanji() {
return banji;
}
public void setBanji(String banji) {
this.banji = banji;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [id=" + id + ", username=" + username + ", sex=" + sex + ", age=" + age + ", banji=" + banji
+ ", hobby=" + hobby + "]";
}
}
6.com.inter包里面写入对student相关操作:
package com.inter;
import java.util.List;
import com.po.Student;
public interface StudentInter {
public List<Student> select();
public void insert(Student student);
public void delete(int id);
public int update(Student student);
Student findwhere(Student student);
}
7.在com.mapper里面通过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指定接口的地址 -->
<mapper namespace="com.inter.StudentInter">
<sql id="select"><!-- id可以自己取 -->
select * from student
</sql>
<select id="select" resultType="Student">
select * from student
</select>
<select id="selectById" parameterType="Integer" resultType="Student">
<include refid="select" /> where id=#{id}
</select>
<insert id="insert" parameterType="Student">
<!-- #{id},#{uname},#{upass},#{sex}是test这个bean中定义的属性名 -->
insert into student(username,sex,age,banji,hobby) values(#{username},#{sex},#{age},#{banji},#{hobby});
</insert>
<delete id="delete" parameterType="int">
delete from student where id=#{id}
</delete>
<update id="update" parameterType="com.po.Student">
update student set username=#{username},sex=#{sex},age=#{age},banji=#{banji},hobby=#{hobby} where id=#{id}
</update>
<select id="findwhere" parameterType="Student" resultType="Student">
<include refid="select" />
<where>
<if test="banji!=null">
banji like '%' #{banji} '%'
</if>
<if test="hobby!=null">
and hobby like #{hobby} '%'
</if>
</where>
</select>
</mapper>
8.在com.test里面编写测试类:
package com.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.inter.StudentInter;
import com.po.Student;
import com.util.DButil;
public class Studentmain {
public static void main(String[] args) {
SqlSession session=DButil.getSession(true);
StudentInter inter=session.getMapper(StudentInter.class);
Student student=new Student();
student.setBanji("20");
student.setHobby("aa");
inter.findwhere(student);
}
@Test
public void insert() {
SqlSession session=DButil.getSession(true);
StudentInter inter=session.getMapper(StudentInter.class);
Student st=new Student();
st.setUsername("吴");
st.setSex("男");
st.setAge("21");
st.setBanji("24班");
st.setHobby("篮球");
inter.insert(st);
}
@Test
public void delete() {
SqlSession session=DButil.getSession(true);
StudentInter inter=session.getMapper(StudentInter.class);
inter.delete(4);
}
@Test
public void update() {
SqlSession session=DButil.getSession(true);
StudentInter inter=session.getMapper(StudentInter.class);
Student st=new Student();
st.setUsername("yang");
st.setSex("男");
st.setAge("23");
st.setBanji("14班");
st.setHobby("running");
st.setId(6);
inter.update(st);
}
@Test
public void select() {
SqlSession session=DButil.getSession(true);
StudentInter inter=session.getMapper(StudentInter.class);
List<Student> list=inter.select();
for (Student st : list) {
System.out.print("序号:"+st.getId()+";姓名:"+st.getUsername()+";sex:"+
st.getSex()+";age:"+st.getAge()+";班级:"+st.getBanji()+";爱好:"+st.getHobby());
}
}
@Test
public void findwhere() {
SqlSession session=DButil.getSession(true);
StudentInter inter=session.getMapper(StudentInter.class);
Student student=new Student();
student.setBanji("20");
student.setHobby("aa");
inter.findwhere(student);
System.out.println(inter.findwhere(student));
}
}