程序结构图:

表结构;

创表sql:

CREATE TABLE `users` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `username` varchar(200) DEFAULT NULL,

  `password` varchar(32) DEFAULT NULL,

  `sex` char(1) DEFAULT NULL,

  `birthday` date DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8

User.xml

<?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">

<!-- namespace命名空间特殊作用: 如果使用mapper动态代理方法,这里就需要配置mapper接口地址-->

<mapper namespace="test">
<!-- 根据用户id查询一条记录(返回单条记录) -->
<!-- 
select标签表示sql查询,内容会封装到Mapped Statement中。
可以将这个select标签称为一个Statement
id:Statement的id,用于标识select中定义的 sql,id是在同一个命名空间中不允许重复
#{}:表示一个占位符,避免sql注入
parameterType:表示输入参数的类型
resultType:表示输出 结果集单条记录映射的java对象类型,select查询的字段名和resultType中属性名一致,才能映射成功。
#{value}:value表示parameter输入参数的变量,如果输入参数是简单类型,使用#{}占位符,变量名可以使用value或其它的名称 
-->
<select id="findUserById" parameterType="int" resultType="com.itheima.mybatis.po.User">
SELECT * FROM users WHERE id = #{id}
</select>
<!-- 查询用户列表(返回list集合) -->
<!-- 
不管结果集查询一条还是多条,resultType指定结果集单条记录映射的java对象类型
${}:表示sql拼接,相当于sql字符串拼接,无法避免sql注入
${value}:value表示parameter输入参数的变量,如果输入参数是简单类型,使用${}拼接符,变量名必须使用value
${value}直接 将value获取到拼接在sql中,value值不加任何修饰
-->
<select id="findUserList" parameterType="String" resultType="com.itheima.mybatis.po.User">
SELECT * FROM users WHERE username LIKE '%${value}%'
</select>

</mapper>

SqlMapConfig.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>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<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/mybatisdb?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>

<!-- 配置mapper映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>

</configuration>

log4j.properties:

# Global logging configuration

log4j.rootLogger=DEBUG, stdout

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

代码:

public class TestMybatis {

@Test
public void testFindUserById() throws Exception {
// 加载配置文件
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 通过配置文件获取到sqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过sessionFactory对象获取到sqlSession对象
SqlSession sqlSession = sessionFactory.openSession();
// 向数据库发起查询(查询单条记录),并接收返回结果
User user = sqlSession.selectOne("test.findUserById", 3);
// 打印结果
System.out.println(user);

// 向数据库发起查询(查询多条记录),并接收返回结果
List<User> list = sqlSession.selectList("test.findUserList", "小");
System.out.println(list.size());
for (User user2 : list) {
System.out.println(user2);
}
}

}

执行结果:

最新文章

  1. Python 读写文件中数据
  2. Raspberry Pi UART with PySerial
  3. rabbitMQ学习(一)
  4. C++容器之Vector
  5. Java final自变量
  6. emacs 新手笔记(一) —— 阅读【emacs tutorial】
  7. selmodel
  8. SpringMVC中Controller跳转到另一个Controller方法
  9. 2dx关于js响应layer触摸消息的bug
  10. Java 如何防止线程意外中止
  11. asp 下拉框二级联动
  12. J - 计算两点间的距离
  13. 【4】JAVA---地址App小软件(UpdatePanel.class)(表现层)
  14. 使用WCF Data Service 创建OData服务
  15. IntelliJ IDEA 2017.1.4 x64配置说明
  16. ASP.NET Core中的OWASP Top 10 十大风险-失效的访问控制与Session管理
  17. BZOJ 3787: Gty的文艺妹子序列 [分块 树状数组!]
  18. 201621123031 《Java程序设计》第8周学习总结
  19. Java开发学习心得(三):项目结构
  20. c++中的new、operator new、placement new

热门文章

  1. sip会话流程以及sip介绍(2)
  2. Neo4j与springdata集成
  3. Nodejs之路(一)—— Nodejs入门
  4. 从0开始学习ssh之资源分类
  5. Nmap扫描原理(下)
  6. [Ceoi2010]Pin
  7. Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
  8. CF1140F - Extending Set of Points
  9. 方法的重写(override)两同两小一大原则:
  10. java关键字一览表