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

黄沙百战穿金甲,不破楼兰终不还。这篇文章主要讲述mybatis框架(入门方法,dao层原始开发方法,mapper代理开发)(sqlserver数据库)相关的知识,希望能为你提供帮助。
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="https://www.songbingjia.com/android/com.microsoft.sqlserver.jdbc.SQLServerDriver" />
< property name="url" value="https://www.songbingjia.com/android/jdbc:sqlserver://localhost:1433; databaseName=JSP" />
< property name="username" value="https://www.songbingjia.com/android/sa" />
< property name="password" value="https://www.songbingjia.com/android/5201314" />
< property name="poolMaximumActiveConnections" value="https://www.songbingjia.com/android/50" />
< /dataSource>
< /environment>
< /environments>

< !-- 加载mapper文件 -->
< mappers>
< mapper resource="./map/select.xml" />
< /mappers>
< /configuration>
 
第二步:编写映射文件    map.xml1.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);
 
 
 
 
【mybatis框架(入门方法,dao层原始开发方法,mapper代理开发)(sqlserver数据库)】 
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);


}
}
 

    推荐阅读