如何配置mybatis開發環境?

Tags: 項目, 環境,

mybatis是apache下的一個開源項目,前身是ibatis。作為一款優秀的ORM對象關係映射框架,在實際的開發項目中應用非常廣泛。

工具/原料

MyEclipse或者Eclipse

SQL Server 2008

mybatis,sql的jar包

方法/步驟

打開MyEclipse或者Eclipse,新建一個JavaProject項目mybatis

如何配置mybatis開發環境

如何配置mybatis開發環境

下載所需jar包右鍵點擊項目依次選擇New--Folder,此時彈出對話框,Folder name填寫lib。複製jar包粘貼到lib文件夾,展開lib文件夾,選中2個jar包,右鍵點擊jar包,依次選擇Build Path--Add to Path。

如何配置mybatis開發環境

如何配置mybatis開發環境

打開SQL Server 2008查詢分析器,創建mybatis數據庫

create database mybatis

選擇mybatis數據庫,創建users表

create table users(

userId int identity(1,1) primary key,

userName varchar(50) not null,

userPassword char(32) not null,

userBal int default 0

)

如何配置mybatis開發環境

在mybatis項目下創建cn.hans.mybatis.domain、cn.hans.mybatis.mapper、cn.hans.mybatis.test

如何配置mybatis開發環境

在cn.hans.mybatis.domain包下創建類User

package cn.hans.mybatis.domain;

public class User {

private Integer userId;

private String userName;

private String userPassword;

// account balance 賬戶餘額

private Integer userBal;

public User(){

}

public Integer getUserId() {

return userId;

}

public void setUserId(Integer userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserPassword() {

return userPassword;

}

public void setUserPassword(String userPassword) {

this.userPassword = userPassword;

}

public Integer getUserBal() {

return userBal;

}

public void setUserBal(Integer userBal) {

this.userBal = userBal;

}

}

如何配置mybatis開發環境

在cn.hans.mybatis.mapper包下創建接口UserMapper

package cn.hans.mybatis.mapper;

import cn.hans.mybatis.domain.User;

public interface UserMapper{

public User selectUser(Integer userId);

public void insertUser(User user);

public void updateUser(User user);

public void deleteUser(Integer userId);

}

配置文件UserMapper.xml

EN" ";>

insert into users (userName,userPassword,userBal) values (#{userName},#{userPassword},#{userBal})

update users set userName=#{userName},userPassword=#{userPassword},userBal=#{userBal} where userId=#{userId}

delete from users where UserId=#{userId}

如何配置mybatis開發環境

如何配置mybatis開發環境

在項目根目錄創建數據庫配置文件db.properties

jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

jdbc.url=jdbc:sqlserver://localhost:1433;databaseName=mybatis

jdbc.username=admin

jdbc.password=admin

mybatis配置文件Configuration.xml

"-//mybatis.org//DTD Config 3.0//EN"

";>

如何配置mybatis開發環境

如何配置mybatis開發環境

在cn.hans.mybatis.test下創建類MybatisTest

package cn.hans.mybatis.test;

import java.io.IOException;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.hans.mybatis.domain.User;

import cn.hans.mybatis.mapper.UserMapper;

public class MybatisTest {

public static void main(String[] args) throws IOException{

SqlSessionFactory ssf=new SqlSessionFactoryBuilder()

.build(Resources.getResourceAsReader("Configuration.xml"));

SqlSession session=ssf.openSession();

UserMapper userMapper=session.getMapper(UserMapper.class);

User user=new User();

user.setUserName("test001");

user.setUserPassword("12345678123456781234567812345678");

user.setUserBal(50);

userMapper.insertUser(user);

session.commit();

session.close();

}

}

如何配置mybatis開發環境

運行MybatisTest,打開SQL Server 2008,查詢mybatis數據庫下數據表users。如果現實如下,則配置成功。

如何配置mybatis開發環境

注意事項

接口UserMapper的方法名與UserMapper.xml配置文件的bean的id相同

相關問題答案