mybatis使用注解开发

1.Pet.jaa

package com.annotation.entity;

import java.util.Date;

/**
 * 创建实体类Pet
 * @author Steven
 *
 */
public class Pet {

	private int id;//宠物id
	private String name;//名称
	private String owner;//主人
	private String species;//品种
	private char sex;//性别
	private Date birth;//出生日期
	private Date death;//死亡日期
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public String getSpecies() {
		return species;
	}
	public void setSpecies(String species) {
		this.species = species;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public Date getDeath() {
		return death;
	}
	public void setDeath(Date death) {
		this.death = death;
	}
	
	@Override
	public String toString() {
		return "Pet [id=" + id + ", name=" + name + ", owner=" + owner + ", species=" + species + ", sex=" + sex
				+ ", birth=" + birth + ", death=" + death + "]";
	}
	
	
}

2.PetDao.java 

public interface PetDao {
    //Pet实体类对应数据库中的表
	@Select("select * from pet")
	public List<Pet> selectAll();
}

3.mybatos-config.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>
	<properties resource="sql.properties"></properties>
	
	<typeAliases>
		<!-- <typeAlias type="com.mybatis.entity.Pet" alias="Pet"/> -->
		<!-- 默认别名是对应的类名 -->
		<package name="com.mybatis.entity"/>
	</typeAliases>
	<!--environments 指mybatis可以配置多个环境默认,default只想默认的环境
		每个sqlSessionFactory对应一个environment
	  -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<!-- UNPOOLED访问数据库时每次都会打开关闭连接
				 POOLED数据库池减少了每次初始化、创建连接、和请求验证的时间	
		 -->
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!-- 定义SQL语句的映射文件 -->
		<mapper resource="com/mybatis/entity/PetMapper.xml" />
        <!-- 对应接口类-->
		<mapper class="com.annotation.dao.PetDao"/>
	</mappers>
</configuration>

4.测试类

public class Test {

	public static void main(String[] args) throws IOException {
		SqlSession session=MyBatisUtil.getSqlSession();
            //Mapper映射相关接口
		PetDao petDao=session.getMapper(PetDao.class);
		List<Pet>pets=petDao.selectAll();
		for(Pet pet:pets) {
			System.out.println(pet);
		}
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值