您当前的位置: 首页 >  mybatis

星夜孤帆

暂无认证

  • 5浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

MyBatis基础-05-mapper动态代理

星夜孤帆 发布时间:2019-04-23 17:53:24 ,浏览量:5

log4j.properties
log4j.rootLogger=debug,console
#log4j.logger.com.monkey1024.dao.StudentDao=debug,concole
#\u63A7\u5236\u53F0\u9644\u52A0\u5668
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%-5p][%d{yyyy-MM--dd HH:mm:ss}]%m%n
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/learnmybatis?useSSL=false
jdbc.user=root
jdbc.password=666666
mybatis.xml




	
	
	
	
	
	
		
		
		
		
	
	
	
    
        
            
            
                
                
                
                
            
        
    
    
        
        
    
StudentMapper.xml



	
	
		INSERT INTO t_student(name,age,score) VALUES (#{name},#{age},#{score})
		
		
			select @@identity
		
	
	
	
	
	
		delete from t_student where id=#{id}
	
	
	
		update t_student set name=#{name},age=#{age},score=#{score} where id=#{id}
	
	
	
	
	
		select id,name,age,score from t_student
	
	
	
		select id,name,age,score from t_student where id=#{id}
	
	
	
		
		
		select id,name,age,score from t_student where name like '%${value}%'
	
	
	
	
		
		
		
	
	
	
	
		select id,name,age,score,password  from t_student where id=#{id}
	 
	
	

Student.java
package com.monkey1024.bean;


//手动的设置别名
//@Alias("stu")
public class Student {
	private int id;
	private String name;
	private int age;
	private double score;
	private String pwd;
	
	
	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public Student(int id, String name, int age, double score) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.score = score;
	}

	public Student(int id, String name, int age, double score, String pwd) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.score = score;
		this.pwd = pwd;
	}
	

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + ", pwd=" + pwd + "]";
	}

	public Student(String name, int age, double score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}

	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId(){
		return id;
	}
	public void setId(int id){
		this.id = id;
	}
	public String getName(){
		return name;
	}
}
StudentDao.java
package com.monkey1024.dao;

import java.util.List;

import com.monkey1024.bean.Student;

public interface StudentDao {
	void  insertStudent(Student student);
	
	void deleteStudent(int id);
	
	void updateStudent(Student student);
	
	List selectAllStudent();
	
	Student selectStudentById(int id);
	
	List selectStudentByName(String name);
	
	Student selectStudentPwd(int id);
}
StudentTest01.java
package com.monkey1024.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.monkey1024.bean.Student;
import com.monkey1024.dao.StudentDao;
import com.monkey1024.util.MyBatisUtil;

public class StudentTest01 {
	
	
	private StudentDao studentDao;
	
	private SqlSession sqlSession;
	
	@Before
	public void init(){
		sqlSession = MyBatisUtil.getSqlSession();
		
		//获取studentDao的对象
		//mapper动态代理
		studentDao = sqlSession.getMapper(StudentDao.class);
	}
	
	/*
	 * 方法执行完成后,需要关闭sqlSession
	 */
	@After
	public void closeSession(){
		if(sqlSession !=null){
			sqlSession.close();
		}
	}
	
	
	@Test
	public void insertStudent(){
		Student student = new Student("jyd",52,98.5);
		
		//id的默认值是0
		System.out.println(student);
		studentDao.insertStudent(student);
		//可以获取到id的值
		System.out.println(student);
	}
	
	@Test
	public void deleteStudent(){
		//删除id是2的数据
		studentDao.deleteStudent(2);
	}
	@Test
	public void updateStudent(){
		Student student = new Student("周杰伦",40,99);
		student.setId(4);
		studentDao.updateStudent(student);
	}
	@Test
	public void selectAllStudent(){
		List students = studentDao.selectAllStudent();
//		System.out.println(students);
		//jdk8新增的foreach方法+lambda表达式
		students.forEach((s->{
			System.out.println(s);
		}));
	}
	@Test
	public void selectStudentById(){
		Student student = studentDao.selectStudentById(7);
		System.out.println(student);
	}
	@Test
	public void selectStudentByName(){
		List students = studentDao.selectStudentByName("j");
		//jdk8新增的foreach方法+lambda表达式
		students.forEach((s->{
			System.out.println(s);
		}));
	}
	@Test
	public void selectStudentPwd(){
		Student student = studentDao.selectStudentPwd(0);
		System.out.println(student);
	}
	
}

 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.1140s