mybatis依赖的jar非常少,主要有两个:
(1)mybatis自身jar,它就只有一个jar包构成,不像spring,有那么一大堆。在早期的版本,mybatis还需要依赖javassist jar(实现动态代理),ognl jar(实现动态sql),而今mybatis已经将javassist和ognl融入了进来,全部整合到mybatis内部,所以目前mybatis自身只有一个jar构成。
(2)数据库驱动jar包,例如mysql-connector-java.jar等。其实,即便仅仅使用JDBC,也得依赖于数据库驱动jar包。
另外,还有两个当做补充的jar包:
(1)日志jar,例如log4j.jar,很多框架都依赖日志jar包,用于打印日志,方便调试。
(2)mybatis-spring jar,可以将MyBatis代码无缝地整合到Spring中。
补充的jar,非必须要用到的,只想跑个mybatis demo,则中需要上面的两个必须jar即可。
MyBatis中文官网
<dependency><groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.5</version>
</dependency>
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项目下创建cn.hans.mybatis.domain、cn.hans.mybatis.mapper、cn.hans.mybatis.test
5、在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
}
}
6、在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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//
EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.hans.mybatis.mapper.UserMapper">
<select id="selectUser" parameterType="Integer" resultType="cn.hans.mybatis.domain.User">
select *from users where userId = #{userId}
</select>
<insert id="insertUser" parameterType="cn.hans.mybatis.domain.User">
insert into users (userName,userPassword,userBal) values (#{userName},#{userPassword},#{userBal})
</insert>
<update id="updateUser" parameterType="cn.hans.mybatis.domain.User">
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、在项目根目录创建数据库配置文件db.properties
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433databaseName=mybatis
jdbc.username=admin
jdbc.password=admin
mybatis配置文件Configuration.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>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/hans/mybatis/mapper/UserMapper.xml" />
</mappers>
</configuration>
8、在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()
}
}
9、运行MybatisTest,打开SQL Server 2008,查询mybatis数据库下数据表users。如果现实如下,则配置成功。
谢谢采纳
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)