idea上MyBatis第一个例子

接着上面创建的maven项目来。

1.java目录下创建cn.happy.entity包

2.idea下创建数据库连接

配置连接参数

3.把数据库表变成实体类

导入成功,改一下包名就可以用了

4.新建cn.happy.dao包,在下面新建IUserDAO接口,代码如下

1 package cn.happy.dao;
2 
3 import cn.happy.entity.Smbms_user;
4 
5 import java.util.List;
6 
7 public interface IUserDAO {
8     List<Smbms_user> findAll();
9 }

5.在IUserDAO同包下新建IUserDAO.xml文件(俗称小配置),文件名一定要相同(这样做可以规避后面的一些问题)。

官方文档中找到

2.1.4 Exploring Mapped SQL Statements

把下面的文档直接粘贴到IUserDAO.xml里

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="org.mybatis.example.BlogMapper">
6  <select id="selectBlog" parameterType="int" resultType="Blog">
7  select * from Blog where id = #{id}
8  </select>
9 </mapper>

修改包名和<mapper>节点,比如我的代码修改如下即可

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!--关联接口的全限定名-->
 6 <mapper namespace="cn.happy.dao.IUserDAO">
 7     <!--大配置中settings autoMappingBehavior NONE,所以数据库只会装配以下column-->
 8     <resultMap id="UserMapper" type="cn.happy.entity.Smbms_user">
 9         <id property="id" column="id"/>
10         <result property="username" column="username"/>
11         <result property="userpassword" column="userpassword"/>
12     </resultMap>
13     <!--id要与接口中的方法名相同-->
14     <select id="findAll" resultMap="UserMapper">
15         select * from smbms_user
16     </select>
17 </mapper>

6.配置resources

这里需要配置三个文件

1.jdbc.properties

2.log4j.properties

3.mybatis-config.xml

jdbc.properties

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql:///t14_smbms?characterEncoding=utf8&useSSL=true
3 jdbc.username=root
4 jdbc.password=root

log4j.properties

 1 log4j.rootLogger=debug,stdout,file
 2 ### direct log messages to stdout ###
 3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 4 log4j.appender.stdout.Target=System.out
 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 6 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 7 
 8 ### direct messages to file mylog.log ###
 9 log4j.appender.file=org.apache.log4j.FileAppender
10 log4j.appender.file.File=c:\\logs\\log.log
11 log4j.appender.file.layout=org.apache.log4j.PatternLayout
12 log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
13 
14 ### set log levels - for more verbose logging change 'info' to 'debug' ###
15 
16 log4j.logger.cn.happy.dao=debug, stdout,file

mybatis-config.xml,俗称大配置

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!--引入 jdbc.properties-->
 7     <properties resource="jdbc.properties"/>
 8     <settings>
 9         <setting name="logImpl" value="LOG4J"/>
10         <setting name="autoMappingBehavior" value="NONE"/>
11     </settings>
12     <!--别名,这里不使用别名,一律使用全限定名-->
13     <!--<typeAliases>
14         <package name="cn.happy.entity"/>
15     </typeAliases>-->
16     <!--DB connection info,配置mybatis多套运行环境-->
17     <environments default="development">
18         <environment id="development">
19             <!--采用jdbc事务管理-->
20             <transactionManager type="JDBC"/>
21             <dataSource type="POOLED">
22                 <property name="driver" value="${jdbc.driver}"/>
23                 <property name="url" value="${jdbc.url}"/>
24                 <property name="username" value="${jdbc.username}"/>
25                 <property name="password" value="${jdbc.password}"/>
26             </dataSource>
27         </environment>
28     </environments>
29     <!--大配置关联小配置-->
30     <mappers>
31         <!--关联单个小配置,不能重复关联-->
32         <mapper resource="cn/happy/dao/IUserDAO.xml"/>
33         <!--关联整个包,dao层接口名字和对应的xml名字要一样-->
34         <!--<package name="cn.happy.dao"/>-->
35     </mappers>
36 </configuration>

7.编写MyBatisUtil类

 1 package cn.happy.util;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 
11 public class MyBatisUtil {
12     private static InputStream is;
13     private static SqlSessionFactory sqlSessionFactory;
14     static {
15         try {
16             is=Resources.getResourceAsStream("mybatis-config.xml");
17         } catch (IOException e) {
18             e.printStackTrace();
19         }
20         sqlSessionFactory= new SqlSessionFactoryBuilder().build(is);
21     }
22     private MyBatisUtil(){}
23     public static SqlSession getSession(){
24         return sqlSessionFactory.openSession();
25     }
26 }

8.创建测试类

代码:

 1 package cn.happy.test;
 2 
 3 import cn.happy.dao.IUserDAO;
 4 import cn.happy.entity.Smbms_user;
 5 import cn.happy.util.MyBatisUtil;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.junit.Test;
 8 
 9 import java.util.List;
10 
11 public class Test20170916 {
12     @Test
13     public void findAll(){
14         SqlSession session = MyBatisUtil.getSession();
15         IUserDAO mapper = session.getMapper(IUserDAO.class);
16         List<Smbms_user> users = mapper.findAll();
17         for (Smbms_user user:users
18              ) {
19             System.out.println(user.getId()+"\t"+user.getUsername()+"\t"+user.getUserpassword());
20         }
21     }
22 }

 运行结果:

 

posted @ 2017-09-16 17:22  Tomas曼  Views(2112)  Comments(0Edit  收藏  举报