如何配置mybatis开发环境

如何配置mybatis开发环境,第1张

1、打开MyEclipse或者Eclipse,新建一个JavaProject项目mybatis

2、下载所需jar包,右键点击项目依次选择New--Folder,此时d出对话框,Folder name填写lib。复制jar包粘贴到lib文件夹,展开lib文件夹,选中2个jar包,右键点击jar包,依次选择Build Path--Add to Path。

3、打开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

)

4、在mybatis项目下创建cnhansmybatisdomain、cnhansmybatismapper、cnhansmybatistest

5、在cnhansmybatisdomain包下创建类User

package cnhansmybatisdomain;

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) {

thisuserId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

thisuserName = userName;

}

public String getUserPassword() {

return userPassword;

}

public void setUserPassword(String userPassword) {

thisuserPassword = userPassword;

}

public Integer getUserBal() {

return userBal;

}

public void setUserBal(Integer userBal) {

thisuserBal = userBal;

}

}

6、在cnhansmybatismapper包下创建接口UserMapper

package cnhansmybatismapper;

import cnhansmybatisdomainUser;

public interface UserMapper{

public User selectUser(Integer userId);

public void insertUser(User user);

public void updateUser(User user);

public void deleteUser(Integer userId);

}

配置文件UserMapperxml

<xml version="10" encoding="UTF-8" >

<!DOCTYPE mapper PUBLIC "-//ibatisapacheorg//DTD Mapper 30//

EN" ">

<mapper namespace="cnhansmybatismapperUserMapper">

  <select id="selectUser" parameterType="Integer" resultType="cnhansmybatisdomainUser">

     select from users where userId = #{userId}

  </select>

  <insert id="insertUser" parameterType="cnhansmybatisdomainUser">

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

  </insert>

  <update id="updateUser" parameterType="cnhansmybatisdomainUser">

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

  </update>

  <delete id="deleteUser" parameterType="Integer">

      delete from users where UserId=#{userId}

  </delete>

</mapper>

7、在项目根目录创建数据库配置文件dbproperties

jdbcdriver=commicrosoftsqlserverjdbcSQLServerDriver

jdbcurl=jdbc:sqlserver://localhost:1433;databaseName=mybatis

jdbcusername=admin

jdbcpassword=admin

mybatis配置文件Configurationxml

<xml version="10" encoding="UTF-8" >

<!DOCTYPE configuration PUBLIC

  "-//mybatisorg//DTD Config 30//EN"

  ">

<configuration>

<properties resource="dbproperties"/>

<environments default="development">

<environment id="development">

<transactionManager type="JDBC"/>

 <dataSource type="POOLED">

   <property name="driver" value="${jdbcdriver}"/>

   <property name="url" value="${jdbcurl}"/>

   <property name="username" value="${jdbcusername}"/>

   <property name="password" value="${jdbcpassword}"/>

 </dataSource>

</environment>

</environments>

<mappers>

      <mapper resource="cn/hans/mybatis/mapper/UserMapperxml" />

</mappers>

</configuration>

8、在cnhansmybatistest下创建类MybatisTest

package cnhansmybatistest;

import javaioIOException;

import orgapacheibatisioResources;

import orgapacheibatissessionSqlSession;

import orgapacheibatissessionSqlSessionFactory;

import orgapacheibatissessionSqlSessionFactoryBuilder;

import cnhansmybatisdomainUser;

import cnhansmybatismapperUserMapper;

public class MybatisTest {

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

SqlSessionFactory ssf=new SqlSessionFactoryBuilder()

build(ResourcesgetResourceAsReader("Configurationxml"));

SqlSession session=ssfopenSession();

      UserMapper userMapper=sessiongetMapper(UserMapperclass);

      User user=new User();

      usersetUserName("test001");

      usersetUserPassword("12345678123456781234567812345678");

      usersetUserBal(50);

userMapperinsertUser(user);

sessioncommit();

sessionclose();

}

}

9、运行MybatisTest,打开SQL Server 2008,查询mybatis数据库下数据表users。如果现实如下,则配置成功。

谢谢采纳

pring配置文件

<xml version="10" encoding="UTF-8">

<beans xmlns=">

可以的。

<update id="exchangePartition" parameterType="javautilMap">

alter table ${tableName} add zzz varchar2(50);

exchange partition ${destinationPartitionName}

with table ${sourceTableName}

including indexes

with validation

</update>

jbpm本身提供了各种数据库的脚本了,你只要使用对应数据库脚本建立数据库就行了

然后修改hibernatecfgxml里关于数据库的连接

<!-- hibernate dialect -->

    <property name="hibernatedialect">orghibernatedialectMySQLInnoDBDialect</property>

    <!-- JDBC connection properties (begin) -->

 <property name="hibernateconnectiondriver_class">commysqljdbcDriver</property>

    <property name="hibernateconnectionurl">jdbc:mysql://localhost:3306/dbname</property>

    <property name="hibernateconnectionusername">root</property>

    <property name="hibernateconnectionpassword">root</property>

    <!-- JDBC connection properties (end) -->

Mybatis是什么

mybatis是一个持久层ORM框架。它内部封装了jdbc,使得开发更简洁,更高效。

MyBatis可以通过xml或注解完成ORM映射关系配置。

Mybatis和JDBC的关系

JDBC是Java提供的一个 *** 作数据库的API; MyBatis是一个持久层ORM框架,底层是对JDBC的封装。

MyBatis对JDBC *** 作数据库做了一系列的优化:

(1) mybatis使用已有的连接池管理,避免浪费资源,提高程序可靠性。

(2) mybatis提供插件自动生成DAO层代码,提高编码效率和准确性。

(3)mybatis 提供了一级和二级缓存,提高了程序性能。

(4) mybatis使用动态SQL语句,提高了SQL维护。(此优势是基于XML配置)

(5) mybatis对数据库 *** 作结果进行自动映射

MyBatis的优点和缺点

优点:

简单:易于学习,易于使用,通过文档和源代du码,可以比较完全zhi的掌握它的设计思路和实现。

实用:提供了数据映射功能,提供了对底层数据访问的封装(例如adonet),提供了DAO框架,可以使我们更容易的开发和配置我们的DAL层。

灵活:通过sql基本上可以实现我们不使用数据访问框架可以实现的所有功能,或许更多。

功能完整:提供了连接管理,缓存支持,线程支持,(分布式)事物管理,通过配置作关系对象映射等数据访问层需要解决的问题。提供了DAO支持,并在DAO框架中封装了ADONET,NHibernate和DataMapper。

增强系统的可维护性:通过提供DAO层,将业务逻辑和数据访问逻辑分离,使系统的设计更清晰,更易维护,更易单元测试。sql和代码的分离,提高了可维护性。

缺点:

sql工作量很大,尤其是字段多、关联表多时,更是如此。

sql依赖于数据库,导致数据库移植性差。

由于xml里标签id必须唯一,导致DAO中方法不支持方法重载。

字段映射标签和对象关系映射标签仅仅是对映射关系的描述,具体实现仍然依赖于sql。(比如配置了一对多Collection标签,如果sql里没有join子表或查询子表的话,查询后返回的对象是不具备对象关系的,即Collection的对象为null)。

DAO层过于简单,对象组装的工作量较大。

不支持级联更新、级联删除。

编写动态sql时,不方便调试,尤其逻辑复杂时。

提供的写动态sql的xml标签功能简单(连struts都比不上),编写动态sql仍然受限,且可读性低。使用不当,容易导致N+1的sql性能问题。

以上就是关于如何配置mybatis开发环境全部的内容,包括:如何配置mybatis开发环境、spring+mybatis怎么配置一个数据源,多个数据库、mybatis sql如何修改表结构等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9761754.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存