创建学生表,利用Mybatis实现学生信息得增删改查

创建学生表,利用Mybatis实现学生信息得增删改查_第1张图片
一、数据库里建表
创建学生表,利用Mybatis实现学生信息得增删改查_第2张图片
第二、打开eclipse建项目
创建学生表,利用Mybatis实现学生信息得增删改查_第3张图片

com.stud.po

package com.stud.po;

public class student {
private int id;
private String username;
private String sex;
private int age;
private String class1;
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 int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getClass1() {
	return class1;
}
public void setClass1(String class1) {
	this.class1 = class1;
}
public String getHobby() {
	return hobby;
}
public void setHobby(String hobby) {
	this.hobby = hobby;
}

public student() {
	super();
}
@Override
public String toString() {
	return "student [id=" + id + ", username=" + username + ", sex=" + sex + ", age=" + age + ", class1=" + class1
			+ ", hobby=" + hobby + "]";
}

}

com.stud.inter



package com.stud.inter;

import java.util.List;

import com.stud.po.student;

public interface StudInter {
	public List<student> select();
	public void insert(student student);
	public int update(student student);
	public void delete(int id);
	student findwhere(student student);
}

src下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>
        <!-- 在控制台输出sql语句 -->
        <settings>
        	<setting name="logImpl" value="STDOUT_LOGGING"/>
        </settings>
          <!--  name实体类的包地址,不需要类名-->
       <typeAliases>
        <package name="com.stud.po"/>
        </typeAliases> 
        <!--  可以配置多个环境(少用),default写啥运行哪个环境-->
        <!-- 指定 下面配置的environment id="mysql"的id的值  -->
        <environments default="mysql">
        <environment id="mysql">
            <!--定义事务transaction/处理事务的名称-->
            <transactionManager type="JDBC">
            </transactionManager>
            <!--定义数据源 :数据库的驱动类地址、密码、端口、用户名-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3307/mybatisdb?characterEncoding=UTF-8"></property>
                <property name="username" value="root"></property>
                <property name="password" value="123456"></property>
            </dataSource>
        </environment>
    </environments>
   <mappers>
   <mapper resource="com/stud/mapper/StudMapper.xml"/>
   </mappers>
    </configuration>

com.stud.util

package com.stud.util;

import java.io.IOException;
import java.io.Reader;

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 {
			//通过字符流读取主控制配置文件到程序里,解析xml文件,获取连接数据库信息。
			Reader reader=Resources.getResourceAsReader("mybatis.xml");
			//创建会话工厂的创造者
			SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
			//得到会话工厂
			factory=builder.build(reader);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	 public static SqlSession getSession(boolean auto){
	        return  factory.openSession(auto);
	    }

}

com.stud.maper

<?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="com.stud.inter.StudInter">
   <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>
	 
	 
    <update id="update" parameterType="student">
	update student set hobby=#{hobby} where id=#{id}
    </update>
    
    <insert id="insert" parameterType="student">
        insert into  student(username,sex,age,class1,hobby) values(#{username},#{sex},#{age},#{class1},#{hobby})
    </insert>
    
    <delete id="delete" parameterType="int">
    delete from student where id=#{id}
    </delete>
    
     <select id="findwhere" parameterType="student" resultType="student">
		 <include refid="select" /><!--  用include进行引用 ,封装了sql语句-->
			
				 <where>
				 	<if test="class1!=null">
				 		 class1 like concat('%' #{class1} '%')
				 	</if>
				 	<if test="hobby!=null">
				 		and hobby like concat( #{hobby} '%')
				 	</if>
				 </where>
					 
	 </select> 
	 
	 <!-- <select id="findwhere" parameterType="student" resultType="student">
		<include refid="select"/>
			<where>
				<choose>
					<when test="class1!=null">
					<bind name="class2" value="'%'+class1+'%'"/>
					class1 like #{class2}
					</when>
					
					<when test="hobby!=null">
					hobby=#{hobby}
					</when>
					
					<otherwise>
					hobby='xxxx'
					</otherwise>
					
				</choose>
			</where>
	</select>  -->
    
    
    </mapper>
    

com.stud.test

package com.stud.test;

import java.util.List;


import org.apache.ibatis.session.SqlSession;


import com.stud.inter.StudInter;
import com.stud.po.student;
import com.stud.util.DBUtil;

public class Studmain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SqlSession session=DBUtil.getSession(true);
		StudInter inter=session.getMapper(StudInter.class);
		/*查询
		 List list=inter.select();
		for (student st : list) {
			System.out.print("序号:"+st.getId()+";姓名:"+st.getUsername()+";sex:"+
		st.getSex()+";age:"+st.getAge()+";班级:"+st.getClass1()+";爱好:"+st.getHobby());
		}*/
		
		
		/*按条件查询
		 * */
		student student=new student();
		student.setClass1("2");
		student.setHobby("篮球");
		inter.findwhere(student);
		System.out.println(inter.findwhere(student));

		/*插入
		 * student st=new student();
		st.setUsername("吴");
		st.setSex("男");
		st.setAge(21);
		st.setClass1("24班");
		st.setHobby("篮球");
		inter.insert(st);*/
		
		/*修改
		 * student st=new student();
		st.setUsername("秀秀");
		st.setSex("男");
		st.setAge(23);
		st.setClass1("14班");
		st.setHobby("running");
		st.setId(6);
		inter.update(st);*/
		
		/*删除
		 * inter.delete(11);
		 * */
	}
	

}

在主方法测试就行。

你可能感兴趣的:(mybatis)