mybatis注解开发---不是主流

先提一嘴,mabatis的注解开发并不是主流,因为他无法设定别名等一系列原因导致他只能用在比较简单的场景里:不过有个好处就是不用写接口配置类了

package com.zsp.dao;

import com.zsp.pojo.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface UserMapper {
     @Select("select * from user")
     public List<User> getUser();
}



<configuration>

    <properties resource="db.properties">properties>
    <settings>
        <setting name="logImpl" value="Log4j"/>
    settings>
    <typeAliases>


        <package name="com.zsp.pojo"/>

    typeAliases>
    <environments default="test">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybaties?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            dataSource>

        environment>
        <environment id="test">
            <transactionManager type="JDBC"/>
            <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><mapper class="com.zsp.dao.UserMapper">mapper> mappers>
configuration>

测试类

package com.zsp.dao;


import com.zsp.pojo.User;
import com.zsp.utils.MybatiesUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UserMapperTest {

    @Test
    public void test(){
        SqlSession session = MybatiesUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.getUser();

        for (User user : users) {
            System.out.println(user);
        }
    }

}

你可能感兴趣的:(mybatis,mysql,java)