JavaWeb——MyBatis入门程序

31 篇文章 0 订阅
30 篇文章 1 订阅

一、引言


一般MyBatis与springMVC常常一起使用,而且与hibernate相比有着天然的优势,继续推进。




     MyBatis应用程序根据XML配置文件创建SqlSessionFactory,SqlSessionFactory在根据配置,配置来源于两个地方,一处是配置文件,一处是Java代码的注解,获取一个SqlSession。SqlSession包含了执行sql所需要的所有方法,可以通过SqlSession实例直接运行映射的sql语句,完成对数据的增删改查和事务提交等,用完之后关闭SqlSession。


二、上代码


文件结构如下:


1、eclipse与tomcat配置不用说,不会的看前面的博文;


2、下载所需的点击打开链接mybatis,还需要mysql的jdbc,添加到lib文件夹


3、添加上述圈红文件,都是重点!!!


4、配置xml文件mapConfig

包括数据库链接和mapper文件

<?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.mysql.jdbc.Driver" />  
                <property name="url" value="jdbc:mysql://localhost:3306/yellowbike" />
                <property name="username" value="root" />  
                <property name="password" value="1234" />
            </dataSource>  
        </environment>  
    </environments>  
     <mappers>
         <mapper resource="map/bike.xml"/>
     </mappers>
</configuration>


5、配置mapper文件

这个配置的是根据id查询数据

<?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="map.bike">
	<select id="findBike" parameterType="java.lang.String" resultType="com.xcy.po.Bike">
	select * from T_BIKE where F_CODE = #{id}
	</select>
	<insert id="addBike" parameterType="com.xcy.po.Bike">
	insert into T_BIKE (F_CODE,F_PW)values(#{F_CODE},#{F_PW})
	</insert>
</mapper>


6、添加mapper对应的po

package com.xcy.po;

public class Bike {

	private int F_ID;
	private String F_CODE;
	private String F_PW;
	public int getF_ID() {
		return F_ID;
	}
	public void setF_ID(int f_ID) {
		F_ID = f_ID;
	}
	public String getF_CODD() {
		return F_CODE;
	}
	public void setF_CODE(String f_CODE) {
		F_CODE = f_CODE;
	}
	public String getF_PW() {
		return F_PW;
	}
	public void setF_PW(String f_PW) {
		F_PW = f_PW;
	}
	@Override
	public String toString() {
		return "Bike [F_ID=" + F_ID + ", F_CODD=" + F_CODE + ", F_PW=" + F_PW + "]";
	}
	
}


7、创建session调用配置好的接口

读取配置文件mapConfig,获得映射关系,然后创建session调用CRUD等方法。

@RequestMapping(value="/getBike",method= RequestMethod.POST)
	public ModelAndView getBike(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Bike bike=new Bike();
		try {		
			InputStream inputStream;
			inputStream = Resources.getResourceAsStream("mapConfig.xml");

			SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			
			String code= request.getParameter("code");
		    bike= sqlSession.selectOne("map.bike.findBike", code);
			System.out.println(bike);
			sqlSession.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("bike", bike);
		modelAndView.setViewName("bikeselect");
		return modelAndView;
	}


三、总结


  • MyBatis基本介绍

  • MyBatis基本环境配置

  • MyBatis入门程序

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值