MyBatis搭建项目
工具包:
创新互联是专业的开平网站建设公司,开平接单;提供网站设计制作、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行开平网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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
7. 在source文件下,新建mapper文件夹,并创建UserInfoMapper.xml文件
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的框架就搭建完毕了.
本文题目:MyBatis搭建项目
本文链接:http://ybzwz.com/article/ggpscp.html