MyBatis语句构造器

2020-03-16  本文已影响0人  煗NUAN

MyBatis语句构造器

<?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-->
    <properties resource="db.properties" />

    <!--起别名使用package标签可以匹配com.mybatis.pojo包下的所有类,并且不区分大小写
        也可使用typeAlias标签匹配指定类型-->
    <typeAliases>
        <package name="com.yanm.pojo" />
    </typeAliases>

    <!--配置mybatis环境变量-->
    <environments default="development">
        <environment id="development">
            <!--配置JDBC事务控制,由mybatis进行管理-->
            <transactionManager type="JDBC"/>
            <!--配置数据源,采用mybatis连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${user}"/>
                <property name="password" value="${pass}"/>
            </dataSource>
        </environment>
    </environments>
    <!--加载映射文件-->
    <mappers>
        <package name="com.yanm.dao"/>
    </mappers>
</configuration>
driver=org.mariadb.jdbc.Driver
url=jdbc:mariadb://localhost:3306/hello
user=root
pass=root
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int uid;
    private String username;
    private String password;
    private int age;
    private String addr;
}
public interface IUserDaoProvider {
    //查询用户信息
    @SelectProvider(type = SQLProvider.class,method = "getAllUser")
    List<User> getAllUsers();

    @SelectProvider(type = SQLProvider.class,method = "getUser")
    List<User> getUsers();

    @InsertProvider(type =SQLProvider.class,method = "saveUser")
    int saveUser();

    @InsertProvider(type =SQLProvider.class,method = "saveUserOne")
    int saveUserOne(User user);
}
public class SQLProvider {
    public String getAllUser(){
        return new SQL()
                .SELECT("uid","username","password")
                .SELECT("age","addr")
                .FROM("user")
                .toString();
    }

    public String getUser(){
        return new SQL()
                .SELECT("uid","username","password")
                .SELECT("age","addr")
                .FROM("user")
                .WHERE("uid=1")
                .OR()
                .WHERE(("username='小乔'"))
                .toString();
    }

    public String saveUser(){
        return new SQL()
                .INSERT_INTO("user")
                .VALUES("username","'yanmeng'")
                .VALUES("password","'yanmeng'")
                .VALUES("age","'18'")
                .VALUES("addr","'yanmeng'")
                .toString();
    }

    public String saveUserOne(User user){
        return new SQL()
                .INSERT_INTO("user")
                .VALUES("username","#{username}")
                .VALUES("password","#{password}")
                .VALUES("age","#{age}")
                .VALUES("addr","#{addr}")
                .toString();
    }
}
public class TestSQL {
    private SqlSession ss=null;

    @Before
    public void beforeSQL(){
        ss= SessionUtils.getSqlSession();
    }

    @After
    public void afterSQL(){
        SessionUtils.SqlSessionClose(ss);
    }

    @Test
    public void getAllUser(){
        IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);

        List<User> users = dao.getAllUsers();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void getUser(){
        IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);

        List<User> users = dao.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void saveUser(){
        IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);

        int i = dao.saveUser();
        System.out.println(i);
    }

    @Test
    public void saveUserOne(){
        IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);
        User user=new User();
        user.setUsername("mengy");
        user.setPassword("mengy");
        user.setAge(49);
        user.setAddr("anhui");
        int i = dao.saveUserOne(user);
        System.out.println(i);
    }
}
public class SessionUtils {
    private static SqlSessionFactory ssf=null;
    private static SqlSession ss=null;

    static {
        try {
            ssf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取SqlSession对象
    public static SqlSession getSqlSession(){
        ss=ssf.openSession(true);
        return ss;
    }

    //关闭SqlSession对象
    public static void SqlSessionClose(SqlSession s){
        if (s!=null){
            s.close();
            s=null;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读