002 Mybatis的入门样例

在001的基础之上我们来执行第一个Mybatis程序

package com.tubai.test;

import com.tubai.dao.UserDao;
import com.tubai.domain.User;
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 java.io.InputStream;
import java.util.List;

/**
 * Mybatis的入门样例
 */
public class MybatisTest01 {
    public static void main(String[] args) throws Exception{
        //1.读取配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(is);
        //3.使用工厂生产SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //5.使用代理对象执行方法
        List<User> userList = userDao.findAll();
        for (User user : userList) {
            System.out.println(user);
        }
        //6.释放资源
        sqlSession.close();
        is.close();
    }
}

可能会出现问题:Error : java 不支持发行版本5

解决方案:

方案一

方案二

<?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.dao.BlogDao"> <resultMap type="Blog" id="blogResult"> <id column="blog_id" property="id" /> <result column="blog_title" property="subject"/> <result column="content" property="content"/> <!-- 映射关联的对象 --> <association property="author" javaType="Author"> <id column="id" property="blog_author_id"/> <result column="userName" property="userName"/> <result column="password" property="password"/> </association> <collection property="comments" ofType="Comment"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <result property="content" column="content"/> </collection> </resultMap> <sql id="where"> <where>userName =#{userName}</where> </sql> <select id="countAll" resultType="int"> select count(*) c from blog; </select> <select id="countSome" resultType="int" parameterType="String"> select count(*) c from blog <include refid="where"/>; </select> <select id="selectAll" parameterType="int" resultMap="blogResult"> select B.id as blog_id, B.subject as blog_title, B.author as blog_author_id, P.id as post_id, P.subject as post_subject, P.content as content from Blog B left outer join Comment P on B.id = P.blogid </select> <insert id="insert" parameterType="com.beans.Blog"> insert into blog(subject,content,author) values(#{subject},#{content},#{author}) </insert> <update id="update" parameterType="com.beans.User"> update blog set subject=#{subject},content=#{content},author=#{author} where userName=#{userName} </update> <delete id="delete" parameterType="int"> delete from blog where subject=#{subject} </delete> </mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值