方式二: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.主配置文件的屬性文件??
?
?