方式二:mapper代理接口方式

这种方式只需要xml+接口(不用写实体类)但是需要符合三个规范

  • 使用mapper'代理接口方式
  • 在同一目录下(可以创建一个源文件夹,达到类文件和xml文件分类的作用)
  • xml中namespace:命名空间为接口的全限定名(包名+类名)
  • xml的文件名和接口的文件名相同,只是后缀名不同
  • 接口中的方法名和xml中sql的id相同

1.创建一个java工程

2.将mysql的jar,mybatis的jar添加到构建路径中

3.写好配置文件

4.连接到数据库

5.写一个和数据库表对应的pojo类(普通的java对象)

6.写一个接口

 1 package impl;
 2 
 3 import java.util.List;
 4 
 5 import entity.Login;
 6 
 7 public interface LoginImpl {
 8     //方法名要与xml文件中的id的唯一标识符相同
 9     List<Login> selectAll();
10     Login selectbyid(int id);
11     int insertone(Login lg);
12     int updateone(Login lg);
13 }

 

7.写对应的xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 4 
 5 <!-- namespace:命名空间, 用于隔离sql语句 -->
 6 <mapper namespace="impl.LoginImpl">
 7 <!-- id是sql语句的唯一标识符,名字要与 接口中的方法名相同 -->
 8 
 9       <select id="selectAll" resultType="Login">
10           select * from t_login 
11       </select>
12       
13       <select id="selectbyid" parameterType="int" resultType="Login">
14           select *from t_login where id =#{id}
15       </select>
16       
17       <insert id="insertone" parameterType="Login" >
18           
19           insert into t_login (username,password) values(#{username},#{password})
20       </insert>
21       
22       <update id="updateone" parameterType="Login">
23           update t_login set username=#{username},password=#{password} where id = #{id}
24       </update>
25       
26       
27 </mapper>

 

8.写测试类

 1 package test;
 2 
 3 
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 
12 import entity.Login;
13 import impl.LoginImpl;
14 
15 public class Login_Test {
16 
17     
18 
19     public static void main(String[] args) throws Exception {
20         // TODO 自动生成的方法存根
21         //读取配置文件
22         String resource = "main_config.xml";
23         InputStream is = Resources.getResourceAsStream(resource);
24         //使用建造者Builder创建sqlsession工厂
25         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
26         //打开session
27         SqlSession sqlSession = sqlSessionFactory.openSession();
28         System.out.println("数据库已经连接"+sqlSession);
29         //使用mapper代理的方式
30          LoginImpl loginImpl = sqlSession.getMapper(LoginImpl.class);
31          /**
32           * 查询所有
33           * List<Login> list = sqlSession.selectList("selectAll");
34           */
35          List<Login> selectAll = loginImpl.selectAll();
36          System.out.println("---------------查询所有用户------------");
37          for (Login login2 : selectAll) {
38             System.out.println("用户id="+login2.getId()+"\t\t用户名="+login2.getUsername()+"\t用户密码="+login2.getPassword());
39         }
40         
41          /**
42           * 通过id查询用户
43           * Login login = sqlSession.selectOne("selectbyid", 3);
44           */
45         System.out.println("-------------通过id查询用户---------------");
46          Login selectbyid = loginImpl.selectbyid(3);
47          System.out.println("通过id查询用户"+selectbyid);
48          
49          /**
50           * 插入一条数据
51           * insert = sqlSession.insert("insertone", login2);
52           */
53          System.out.println("-------------插入一条数据-----------");
54          Login login2 = new Login();
55          login2.setUsername("xy");
56          login2.setPassword("315364");
57          int insertone = loginImpl.insertone(login2);
58          System.out.println("插入一条数据成功"+insertone);
59          
60          /**
61           * 更新一条数据
62           * sqlSession.update("updateone", login3);
63           */
64          System.out.println("--------------更新一条数据------------");
65         Login login3 = new Login( 3, "333333","dhao");
66         int updateone = loginImpl.updateone(login3);
67         System.out.println("更新一条数据成功"+updateone);
68         //提交数据到数据库
69         sqlSession.commit();
70         //关闭会话
71         sqlSession.close();
72         
73         
74         
75         
76 
77     }
78 
79 }

 

9.项目的结构

 

10.mybatis的主配置文件  文件名:main_config.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3  "http://mybatis.org/dtd/mybatis-3-config.dtd" >
 4 <configuration>
 5 <!-- 别名标签 -->
 6 <!--把鼠标移到configuration标签上面会出现以下信息:原因是因为引入了dtd约束文件 Element : configuration
 7 Copyright 2009-2016 the original author or authors. Licensed under the Apache License, Version 2.0 
 8  (the "License"); you may not use this file except in compliance with the License. You may obtain a copy 
 9  of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or 
10  agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, 
11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License 
12  for the specific language governing permissions and limitations under the License.
13 
14 Content Model : (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, 
15  objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)  -->
16  
17  <!-- 引入配置文件 属性标签 -->
18 <properties resource="mapper/main_config.properties"></properties>
19 
20 <!-- 别名    在单个配置文件中 写别名即可-->
21 <typeAliases>    
22     <typeAlias type="entity.Login" alias="login"/>
23     <!-- 自动生成包下面的所有类的别名,别名就是类名,并且首字母大小写都可以使用 -->
24     <package name="impl"/>
25 </typeAliases>
26 <!-- 使用哪个小环境  default就等于哪个小环境的id -->
27     <environments default="development">
28     
29         <environment id="development">
30             <!-- jdbc事务管理 -->
31             <transactionManager type="JDBC"></transactionManager>
32             <!-- 数据库连接池 -->
33             <dataSource type="POOLED">
34                 <property name="driver" value="${driver}"/>
35                 <!-- localhost可以用127.0.0.1或者本机的ip地址表示 -->
36                 <property name="url" value="${url}"/>
37                 <property name="username" value="${username}"/>
38                 <property name="password" value="${password}"></property>
39             </dataSource>
40         </environment>
41         
42         <environment id="mysql">
43             <!-- jdbc事务管理 -->
44             <transactionManager type="JDBC"></transactionManager>
45             <!-- 数据库连接池 -->
46             <dataSource type="POOLED">
47                 <property name="driver" value="${driver}"/>
48                 <!-- localhost可以用127.0.0.1或者本机的ip地址表示 -->
49                 <property name="url" value="${url}"/>
50                 <property name="username" value="${username}"/>
51                 <property name="password" value="${password}"></property>
52             </dataSource>
53         </environment>
54     </environments>
55     
56     <!-- 引入单个映射配置文件  *号表示0个或者多个 -->
57     <mappers>
58         <mapper resource="mapper/login_mapper.xml"/>
59         <!-- <package name="impl"/> -->
60     </mappers>
61 </configuration>

 

11.主配置文件的属性文件  

 

 

posted on 2018-04-25 21:47  邢逸  阅读(259)  评论(0编辑  收藏  举报