设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4761|回复: 2

[网络编程] mybatis深入学习(@ 基于注解--整合Spring框架)

[复制链接]

20

主题

0

好友

351

积分

版主

Rank: 7Rank: 7Rank: 7

性别
保密

最佳新人 活跃会员 热心会员 宣传达人 优秀版主 论坛元老

发表于 2013-10-19 13:40:03 |显示全部楼层
作者:liugenhua 日期:2013-10-19 原文地址:http://www.skywj.com/thread-9233-1-1.html


之前在csdn发布了mybatis的入门学习帖---http://bbs.csdn.net/topics/390605626

开发工具:eclipse 所需jar包 mysql-connector-java-5.1.7-bin.jar(mysql连接jar包) mybatis-3.2.3-SNAPSHOT.jar(mybatis核心jar包)mybatis-spring-1.1.1.jar(和Spring整合必须的) 其他jar包(主要是Springweb框架jar包 官网有下载)

同样以一个实体类为例讲述---
简单用个实体类吧--

public class Student(){
      private String sno;//学号
      private Stirng sname;//姓名
      private String sex;//性别
     /************相关get,set方法省略********/
}

这是基于注解spring配置

<!-- Spring 配置 -->
  <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/classes/ITschool-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>spring</servlet-name>
   <url-pattern>*.html</url-pattern>
  </servlet-mapping>

ITschool-servlet.xml,这个文件名字定义 内容如下:-->

<?xml version="1.0" encoding="UTF-8"?>
<!-- ****************************************************
         ******
    ******************************************************** -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:component-scan base-package="org.ITschool">
        <context:include-filter type="regex" expression=".*.controller"/>
     </context:component-scan>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/Views/"/>
       <property name="suffix" value=".jsp"/>
    </bean>
</beans>

dao接口类实现细节(就以插入记录为例):

public interface StudentDao{
     @Insert("insert into student(sno,sname,sex) values(#{sno},#{sname},#{sex})")
     public boolean addStudentInfos(Student student);
}

Spring 核心参数文件(SpringIt.xml)

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 解析数据资源文件的配置 -->

    <bean id="propertyConfigure"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpathataSource.properties</value>
            </list>
        </property>
    </bean>

<!-- 创建数据源     -->

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="maxIdle" value="${maxIdle}" />
         
    </bean>

<!-- 创建session工厂        -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--  daobean的配置 -->

        <bean id="StudentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="自己定义的Studentdao接口的package路径(包名)" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>

最关键的一步了 就是调用了(实现数据的插入)客户端代码

   context=new ClassPathXmlApplicationContext("SpringIt.xml");(这个SpringIt.xml在src根路径下,内容就是上面的配置)
   StudentDao studao=(StudentDao)context.getBean("StudentDao");

//实现数据的插入。。。。

     Student stu=new Student();
     stu.setSno("123");
    stu.setSname("张三");
    stu.setSex("男");
   studao.addStudentInfos(stu);
   //OK执行完成!数据插入到数据库、

/*************优势:我们只需写sql语句具体实现交给mybatis框架完成***********************/

有问题留言 我们共同讨论....


200

主题

3

好友

2253

积分

管理员

Rank: 9Rank: 9Rank: 9

性别
保密

热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 荣誉管理 论坛元老 最佳新人 活跃会员

发表于 2013-10-22 08:42:09 |显示全部楼层
支持这样的原创技术作品!
回复

使用道具 评分 举报

58

主题

10

好友

644

积分

版主

Rank: 7Rank: 7Rank: 7

性别
保密

最佳新人 活跃会员 突出贡献 优秀版主 论坛元老

发表于 2013-10-22 08:46:33 |显示全部楼层
天下事有难易乎?需要重新思考
回复

使用道具 评分 举报

您需要登录后才可以回帖 登录 | 立即注册


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-3-29 00:30 , Processed in 0.120599 second(s), 27 queries .

回顶部