MyBaits 入门之MyBatis环境搭建(第一篇)

网友投稿 212 2023-06-24


MyBaits 入门之MyBatis环境搭建(第一篇)

MyBatis 简介

MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis。

MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录。

下面分步骤给搭建介绍mybatis 环境搭建。

1)在工程下引入mybatis-3.4.1.jar包,再引入数据库(mysql,mssql..)包.

2)在src下新建一个配置文件conf.xml

<!-- 引入外部配置文件 -->

这里是通过外部配置文件来存储数据库存信息的,所以加入一个jdbc.properties的数据库存信息

driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

url=jdbc:sqlserver://127.0.0.1;databaseName=test

username=sa

password=123456

3)建立一个映射文件 userMapper.xml

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from MS_User

Select * From Ms_User Where id=#{id}

(这里要注意的是,namespace属性,因为,此实例是通过接口映射的方式,所以,namespace属性一点要写成接口的路径)附图:

4)建一个映射接口类

package hw.com.ser;

import java.util.List;

import hw.com.pojo.User;

public interface IUser {

public List queryUsers();

public User queryUserById(int id);

}

5)建立一个SqlSessionFactory

package hw.com.util;

import java.io.IOhttp://Exception;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.util.Properties;

import org.apache.ibatoCvABJJARpis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionFactoryUtil {

private static SqlSessionFactory sqlSessionFactory = null;

private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class;

private SqlSessionFactoryUtil() {

}

public static SqlSessionFactory initSqlSessionFactory() {

String resource = "conf.xml";

InputStream inputStream = null;

try {

inputStream = Resources.getResourceAsStream(resource);

} catch (IOException e) {

e.printStackTrace();

}

synchronized (CLASS_LOCK) {

if (sqlSessionFactory == null) {

sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

}

}

return sqlSessionFactory;

}

public static SqlSession openSqlSession(){

if(sqlSessionFactory==null){

initSqlSessionFactory();

}

return sqlSessionFactory.openSession();

}

}

6)建一个pojo

package hw.com.pojo;

import java.util.Date;

public class User {

private String Id;

private String UserName;

private String UserPwd;

private int DeptmentId;

private String UserTrueName;

private String Email;

private int LearnCenterId;

private Date CreateDate;

private Date LastModifyDate;

private int UserStatus;

public User() {

super();

// TODO Auto-generated constructor stub

}

public String getId() {

return Id;

}

public void setId(String id) {

Id = id;

}

public String getUserName() {

return UserName;

}

public void setUserName(String userName) {

UserName = userName;

}

public String getUserPwd() {

return UserPwd;

}

public void setUserPwd(String userPwd) {

UserPwd = userPwd;

}

public int getDeptmentId() {

return DeptmentId;

}

public void setDeptmentId(int deptmentId) {

DeptmentId = deptmentId;

}

public String getUserTrueName() {

return UserTrueName;

}

public void setUserTrueName(String userTrueName) {

UserTrueName = userTrueName;

}

public String getEmail() {

return Email;

}

public void setEmail(String email) {

Email = email;

}

public int getLearnCenterId() {

return LearnCenterId;

}

public void setLearnCenterId(int learnCenterId) {

LearnCenterId = learnCenterId;

}

public Date getCreateDate() {

return CreateDate;

}

public void setCreateDate(Date createDate) {

CreateDate = createDate;

}

public Date getLastModifyDate() {

return LastModifyDate;

}

public void setLastModifyDate(Date lastModifyDate) {

LastModifyDate = lastModifyDate;

}

public int getUserStatus() {

return UserStatus;

}

public void setUserStatus(int userStatus) {

UserStatus = userStatus;

}

@Override

public String toString() {

return "User [Id=" + Id + ", UserName=" + UserName + ", UserPwd=" + UserPwd + ", DeptmentId=" + DeptmentId

+ ", UserTrueName=" + UserTrueName + ", Email=" + Email + ", LearnCenterId=" + LearnCenterId

+ ", CreateDate=" + CreateDate + ", LastModifyDate=" + LastModifyDate + ", UserStatus=" + UserStatus

+ "]";

}

}

7) 在main方法下做测试

package hw.com.Day1.main;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import hw.com.pojo.User;

import hw.com.ser.IUser;

import hw.com.util.SqlSessionFactoryUtil;

public class UserTest {

public static void main(String[] args) {

SqlSession sqlSession=null;

try {

sqlSession=SqlSessionFactoryUtil.openSqlSession();

IUser iUser=shttp://qlSession.getMapper(IUser.class);

List users=iUser.queryUsers();

if(users.size()>0){

for (User user : users) {

System.out.println(user.toString());

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue项目中做编辑功能传递数据时遇到问题的解决方法
下一篇:BootStrap Table 获取同行不同列元素的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~