您好,登錄后才能下訂單哦!
工具包:
Netbeans8
Maven
MyBatis
項目源碼:https://github.com/sun2shadow/simpleMybatis
使用netbeans創建項目,選擇文件->新建項目->Maven->Web應用程序;
打開項目目錄,在依賴關系上右擊->添加依賴關系->查詢框輸入mysql->選擇mysql:mysql-connector-java->點開選擇對應的mysql驅動版本.
創建數據庫和表
create database foretaste; use foretaste create table user_info(id int(11) not null primary key auto_increment, nickname varchar(50) not null, phone_num char(11) not null, created_time timestamp not null default current_timestamp, last_update_time timestamp not null default current_timestamp);
4. 創建UserInfo的實體
package com.shadow.foretaste.entity; import java.util.Date; /** * * @author sunny */ public class UserInfo { private int id; private String nickname; private String phoneNum; private Date createdTime; private Date lastUpdateTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public Date getCreatedTime() { return createdTime; } public void setCreatedTime(Date createdTime) { this.createdTime = createdTime; } public Date getLastUpdateTime() { return lastUpdateTime; } public void setLastUpdateTime(Date lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; } @Override public String toString() { return "UserInfo[id=" + id; } }
5. 創建UserInfoDao,必須先創建一個Dao接口,用于mapper綁定時指明的namspace
package com.shadow.foretaste.dao; import com.shadow.foretaste.entity.UserInfo; /** * * @author sunny */ public interface UserInfoDao { /** * 根據Id查詢用戶信息 * @param id * @return */ UserInfo getUserInfoById(int id); }
6. 點開->項目的其他源->src/main/source,在默認包上右擊,xml文件,命名為mybatis-config.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> <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/foretaste?zeroDateTimeBehavior=convertToNull "/> <property name="username" value="root"/> <property name="password" value="mysql123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserInfoMapper.xml" /> </mappers> </configuration>
7. 在source文件下,新建mapper文件夾,并創建UserInfoMapper.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"> <mapper namespace="com.shadow.foretaste.UserInfoDao"> <select id="getUserInfoById" parameterType="int" resultType="com.shadow.foretaste.entity.UserInfo"> select * from user_info where id = #{id} </select> </mapper>
8. 創建MyBatisUtils獲取sqlSession
package com.shadow.foretaste.util; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * * @author sunny */ public class MyBatisUtils { private static SqlSessionFactory factory = null; //初始化session工廠 public static void initFactory() throws Exception{ InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(inputStream); } /** * 獲取sqlSession會話 * @return */ public static SqlSession getSession(){ if(null == factory){ try { initFactory(); } catch (Exception ex) { ex.printStackTrace(); } } return factory.openSession(); } }
8. 創建測試方法驗證配置
import com.shadow.foretaste.util.MyBatisUtils; import org.apache.ibatis.session.SqlSession; import static org.junit.Assert.assertNotNull; import org.junit.Test; /** * * @author sunny */ public class TesMyBatis { @Test public void testMyBatis(){ SqlSession session = MyBatisUtils.getSession(); assertNotNull(session); if(session != null){ session.close(); } } }
好了,到此myBatis的框架就搭建完畢了.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。