MyBatis中的全局配置文件

目录:

  • 如何xml,sql语句传值

  • 返回集合List,resultTYPE为集合中元素类全名

  • 返回单个对象resultTYPE vo类全名

首先先做一些准备工作:

1.配置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>
    <environments default="dev">
        <environment id="dev">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="student.xml"></mapper>
    </mappers>
</configuration>

2.导入jar包

3.把工程建成类似这样

4.创建 Student类

package com.jd.student.vo;

public class Student {

	private String id;
	private String name;
	private String mobile;
	private String address;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}

1.通过一个参数,确定一个对象:

  IStudentDao中:

public interface IStudentDao {
        //getName方法,通过id传参
	String getName(String id);
	
}

student.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.jd.student.dao.IStudentDao">

    <select id="getName" resultType="java.lang.String"> //id处写方法名
        select name from stu where id=#{id}
    </select>
</mapper>

Test中:

public class Test {

	public static void main(String[] args) throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
                //传入数据库中对象的id
		System.out.println(studentDao.getName("328cc546-03d4-4e90-8b22-1ed66ffac217"));
		sqlSession.close();
	}
}

2.通过两个参数,确定一个对象

  IStudentDao中:

public interface IStudentDao {

        //传多个参数时要加上@Param
	String getName(@Param("id")String id,@Param("mobile")String mobile);
	
}

 student.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.jd.student.dao.IStudentDao">

    <select id="getName" resultType="java.lang.String">
        select name from stu where id=#{id} and mobile=#{mobile}
    </select>
</mapper>

Test中: 

public class Test {

	public static void main(String[] args) throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
                //传入id,mobile
		System.out.println(studentDao.getName("328cc546-03d4-4e90-8b22-1ed66ffac217","123"));
		sqlSession.close();
	}
}

3.如果数据库和class类不照应:

  IStudentDao中:

public interface IStudentDao {

	List<Student> select(String name);
}

  student.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.jd.student.dao.IStudentDao">
	
    <resultMap type="com.jd.student.vo.Student" id="stu">
        <result column="id" property="id"/>    <!-- column是数据库里面的,property是Student类里面的属性 -->
		<result column="user_name" property="name"/>
		<result column="mobile" property="mobile"/>
		<result column="address" property="address"/>
	</resultMap>

    <select id="select" resultMap="stu">
        select id,user_name,mobile,address from student where user_name like #{name}
    </select>

</mapper>

Test中:

public class Test {

	public static void main(String[] args) throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
		System.out.println(studentDao.getClass().getName());
		for(Student student:studentDao.select("王%")) {
			System.out.println(student.getName());
		}
		sqlSession.close();
	}
}

 

 

 

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值