mabits实现简单的增删改查

1.创建Maven工程
2.在pom.xml中配置所需要的jar包
		
			junit
			junit
			4.12
			test
		
		
			org.mybatis
			mybatis
			3.4.6
		
		
			log4j
			log4j
			1.2.17
		
		
			commons-logging
			commons-logging
			1.2
		
		
			mysql
			mysql-connector-java
			5.1.38
		

3.在resources中创建配置文件
目录如图所示:

mabits实现简单的增删改查_第1张图片
sqlMapConfig中的配置:




	
		
		
		
		
	


	
		
			
			
				
				
				
				
			
		
	
	
	
	
	
		
	
UserMapper.xml




	

	
		insert into stuInfo(stuNo,stuName,gender,age) values(#{stuNo},#{stuName},#{gender},#{age})
	
	
	
		delete from stuInfo where stuNo = #{stuNo}
	
	
	
		update stuInfo set stuName=#{stuName},gender=#{gender},age=#{age} where stuNo=#{stuNo}
	
4.编写java代码
创建UserInfo类

package com.zx.mybatis.sayHello.entity;

public class UserInfo {
	
	private String stuNo;
	
	private String stuName;
	
	private String gender;
	
	private int age;

	public String getStuNo() {
		return stuNo;
	}

	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "UserInfo [stuNo=" + stuNo + ", stuName=" + stuName + ", gender=" + gender + ", age=" + age + "]";
	}
	
	
}


创建单元测试类
package com.zx.mybatis.sayHello.hello.first;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.Before;
import org.junit.Test;

import com.zx.mybatis.sayHello.entity.UserInfo;

public class MybatisTest {
	//构建session工厂
	private SqlSessionFactory ssf;
	
	private Log log = LogFactory.getLog(MybatisTest.class);
	
	/*
	    加载mybatis配置文件
	*/
	@Before
	public void init() {
		try {
			ssf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis/sqlMapConfig.xml"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 添加信息
	 * 
	*/
	@Test
	public void insertStu() {
		//创建能执行映射文件中sql的sqlsession
		SqlSession ss = ssf.openSession();
		
		UserInfo ui = new UserInfo();
		ui.setStuNo("2016010");
		ui.setStuName("詹姆斯");
		ui.setGender("男");
		ui.setAge(33);
		
		int affectedRows = ss.insert("userInfo.insertUserInfo", ui);
		
		log.info("--------"+affectedRows);
		ss.commit();
		ss.close();
	}
	/*
	 * 删除学生信息
	*/
	@Test
	public void deleteStu() {
		SqlSession ss = ssf.openSession();
		UserInfo ui = new UserInfo();
		ui.setStuNo("2017002");
		int affectedRows = ss.delete("userInfo.deleteUserInfo", ui);
		
		log.info("--------"+affectedRows);
		ss.commit();
		ss.close();
	}
	/*
	 * 修改学生信息
	*/
	@Test
	public void updateStu() {
		SqlSession ss = ssf.openSession();
		UserInfo ui = new UserInfo();
		ui.setStuName("刘达");
		ui.setGender("女");
		ui.setAge(10);
		ui.setStuNo("2016003");
		int affectedRows = ss.update("userInfo.updateUserInfo", ui);
		
		log.info("----------"+affectedRows);
		ss.commit();
		ss.close();
	}
	/*
	 * 查询学生信息,动态查询 ,sql分页
	*/
	@Test
	public void selectTest() {
		SqlSession ss = ssf.openSession();
		UserInfo ui = new UserInfo();
		ui.setStuName("张");
		List stuList = ss.selectList("userInfo.selectUserInfoByAttr",ui);
		log.info("------------"+stuList);
		ss.close();
	}
	
	
	
	
	
}	

注意动态查询,配置文件中为

	 
	 
	 
	 
	 
	 	
	 		
	 			and stuName like '%${stuName}%'
	 			
	 	
	 

注意:namespace表示命名空间,对数据库的SQL信息进行分类话管理;
 
  id:表示sql语句的唯一身份,也称为statement ID
  
  resultType:表示sql执行输出结果
 
  parameterType:表示执行sql输入参数
 
  #{id} 表示一个占位符, id 表示接受输入参数的名称。
 
  ${} 与  #{}:${}表示sql 拼接字符串,如果参数类型为基本数值类型,则必须为${value},如果为引用类型,则写属性名称。


你可能感兴趣的:(mabits实现简单的增删改查)