标签:static print inf factor cep stat 结果 src color
import java.util.List; public interface UserMapper { /** * 根据Id查询用户信息 */ public User queryUserById(Long id); /** * 查询所有用户信息 */ public List<User> queryUserAll(); }
<?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:命名空间,随便写,一般保证命名空间唯一 ,为了使用接口动态代理,这里必须是接口的全路径名--> <mapper namespace="com.mybatis.UserMapper"> <!-- statement,内容:sql语句。 id:唯一标识,随便写,在同一个命名空间下保持唯一,使用动态代理之后要求和方法名保持一致 resultType:sql语句查询结果集的封装类型,使用动态代理之后和方法的返回类型一致;resultMap:二选一 parameterType:参数的类型,使用动态代理之后和方法的参数类型一致 --> <select id="queryUserById" resultType="com.mybatis.User"> select * from tb_user where id = #{id} </select> <select id="queryUserAll" resultType="com.mybatis.User"> select * from tb_user </select> </mapper>
<mappers> <mapper resource="mappers/UserMapper.xml"/> </mappers>
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;
public class MybatisTest2 {
public static void main(String[] args) throws Exception {
// 指定全局配置文件
String resource = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.queryUserById(1);
System.out.println(user);
} finally {
sqlSession.close();
}
}
}
运行结果:
User(id=1, userName=zpc, password=123456, name=鹏程, age=22, sex=1, birthday=Sun Sep 02 00:00:00 CDT 1990, created=2020-04-19 11:14:40.0, updated=2020-04-19 11:14:40.0)
标签:static print inf factor cep stat 结果 src color
原文地址:https://www.cnblogs.com/jvStarBlog/p/12730973.html