码迷,mamicode.com
首页 > 移动开发 > 详细

mybatis框架(入门方法,dao层原始开发方法,mapper代理开发)(sqlserver数据库)

时间:2020-02-11 09:48:42      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:dev   占位符   style   driver   factory   属性   info   sqlserver   soft   

1.入门方法

第一步:mybatis全局环境配置  configurs.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>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=JSP" />
<property name="username" value="sa" />
<property name="password" value="5201314" />
<property name="poolMaximumActiveConnections" value="50" />
</dataSource>
</environment>
</environments>

<!-- 加载mapper文件 -->
<mappers>
<mapper resource="./map/select.xml" />
</mappers>

</configuration>

 

第二步:编写映射文件   map.xml

1.mapper中的namespace在一般使用和dao层原始开发可以随意起名,在mapper代理有特殊要求

2.parameterType,输入占位符的类型

3.#{} 占位符,里面的名字随意,输出类型为javabean类型时,里面的参数名字等于javabean的属性名

4.resultType,输出类型

5.map.xml需要在 configurs.xml  文件中的<mappers>标签中声明

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="test" >

<!--查询 -->
<select id="selectUser" parameterType="String" resultType="JavaBean.information">
select * from information where id= #{id}
</select>
<!-- 删除 -->
<delete id="DeleteUsre" parameterType="String">
delete from information where id=#{deleteid}
</delete>
<!-- 插入 -->
<insert id="InsertUser" parameterType="JavaBean.information">
insert into information(id,name,tel,acc,pass) values(#{id},#{name},#{tel},#{acc},#{pass})
</insert>
</mapper>

 

第三步:建java类,用会话工厂操作映射文件

关键代码:

//configurs.xml的位置
String res="mybatisconfigurs/mybatis.xml";

InputStream inp= Resources.getResourceAsStream(res);
//建立工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inp);

SqlSession session = factory.openSession();

 

 

查询用户方法的代码:

//查询用户
public information selectuesr(String id) throws IOException {

//configurs.xml的位置
String res="mybatisconfigurs/mybatis.xml";

InputStream inp= Resources.getResourceAsStream(res);
//建立工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inp);

SqlSession session = factory.openSession();

information inf= session.selectOne("test.selectUser", id);//map.xml namespacec+SQLid

session.commit();

session.close();

return inf;

}

 

2.dao层原始开发

一:编写dao接口

package dao;

import JavaBean.information;

public interface dao {
//查询用户
public information SelestUser(String id);
//添加用户
public void InsertUser(information inf);
//删除用户
public void DeleteUser(String id);

}

 

二:编写接口类

package dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import JavaBean.information;

public class usedao implements dao{

SqlSessionFactory factory;

public usedao( SqlSessionFactory factory) {
this.factory=factory;
}

//查询用户
public information SelestUser(String id) {
SqlSession session = factory.openSession();
information inf= session.selectOne("test.selectUser", id);
session.commit();
session.close();
return inf;

}

@Override
public void InsertUser(information inf) {
SqlSession session = factory.openSession();

session.insert("test.InsertUser", inf);

session.commit();

session.close();

System.out.println("插入成功");

}

@Override
public void DeleteUser(String id) {

SqlSession session = factory.openSession();

session.delete("test.DeleteUsre", id);

session.commit();

session.close();

System.out.println("删除成功");

}

}

 

三:使用接口类

提供工厂实类

String res="mybatisconfigurs/mybatis.xml";

InputStream inp= Resources.getResourceAsStream(res);

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inp);

 

 

 

 

 

package dao;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import JavaBean.information;

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

String res="mybatisconfigurs/mybatis.xml";

InputStream inp= Resources.getResourceAsStream(res);

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inp);

usedao dao=new usedao(factory);

dao.DeleteUser("445201199901100075");






}

}

3.mapper动态代理

4大规范

映射文件map.xml中的<mapper namespace="mapper.usemap" >   namespace=dao接口类的位置

dao接口类的方法的方法名于SQLid一样

dao接口类的方法返回类型于SQL一样

dao接口类的方法参数于SQL一样

一.编写映射文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="mapper.usemap" >

<!--查询 -->
<select id="SelectUser" parameterType="String" resultType="JavaBean.information">
select * from information where id= #{id}
</select>
</mapper>

二.编写接口类

package mapper;

import JavaBean.information;

public interface usemap {


public information SelectUser(String id)throws Exception;

}

三. 使用接口类

package dao;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import JavaBean.information;

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

String res="mybatisconfigurs/mybatis.xml";

InputStream inp= Resources.getResourceAsStream(res);

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inp);

usedao dao=new usedao(factory);


}

}

 

mybatis框架(入门方法,dao层原始开发方法,mapper代理开发)(sqlserver数据库)

标签:dev   占位符   style   driver   factory   属性   info   sqlserver   soft   

原文地址:https://www.cnblogs.com/miwujun/p/12262343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!