MyBatis基础-05-mapper动态代理

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

<?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>

	<!-- 注册db配置文件 -->
	<properties resource="db.properties"/>
	
	<!-- mybatis配置文件中的标签是有顺序的 -->
	<!-- 注册实体类的权限定义名的别名 -->
	<typeAliases>
		<!-- 方式一:这种方式配置不太方便 -->
		<typeAlias type="com.monkey1024.bean.Student" alias="student"/>
		<!-- 方式二:mybatis会在这个包下搜索需要的javabean -->
		<package name="com.monkey1024.bean"/>
	</typeAliases>
	
	
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.user}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--注册映射文件-->
        <mapper resource="com/monkey1024/dao/StudentMapper.xml"/>
    </mappers>
</configuration>

StudentMapper.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">
<mapper namespace="com.monkey1024.dao.StudentDao">
	<!-- parameterType可省略 -->
	<insert id="insertStudent" parameterType="student">
		INSERT INTO t_student(name,age,score) VALUES (#{name},#{age},#{score})
		<!-- 获取自增的主键 -->
		<selectKey resultType="int" keyProperty="id" order="AFTER">
			select @@identity
		</selectKey>
	</insert>
	
	
	<!-- 这里的#{xxx}只是起着占位符的作用 -->
	<delete id="deleteStudent">
		delete from t_student where id=#{id}
	</delete>
	
	<update id="updateStudent">
		update t_student set name=#{name},age=#{age},score=#{score} where id=#{id}
	</update>
	
	<!-- resultType要写上单条数据对应的类 -->
	<!-- 查询中尽量不要出现*会影响sql的执行效率 -->
	<select id="selectAllStudent" resultType="student">
		select id,name,age,score from t_student
	</select>
	
	<select id="selectStudentById" resultType="student">
		select id,name,age,score from t_student where id=#{id}
	</select>
	
	<select id="selectStudentByName" resultType="student">
		<!-- select id,name,age,score from t_student where name like '%' #{name} '%' -->
		<!-- 有sql注入问题 -->
		select id,name,age,score from t_student where name like '%${value}%'
	</select>
	
	<!-- 结果映射 -->
	<resultMap id="studentMapper" type="student">
		<id column="id" property="id"/>
		<result column="password" property="pwd"/>
		<result column="age" property="age"/>
	</resultMap>
	
	
	<select id="selectStudentPwd" resultMap="studentMapper">
		select id,name,age,score,password  from t_student where id=#{id}
	</select> 
	
	
<!-- 	<select id="selectStudentPwd" resultType="student">
		select id,name,age,score,password pwd from t_student where id=#{id}
	</select> -->
</mapper>

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<Student> selectAllStudent();
	
	Student selectStudentById(int id);
	
	List<Student> 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<Student> 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<Student> 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);
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值