mybatis深入学习(@ 基于注解--整合Spring框架)

本文通过一个简单的学生信息管理实例,详细介绍了如何将MyBatis与Spring框架进行整合,包括开发环境搭建、核心配置文件设置及DAO层的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前在csdn发布了mybatis的入门学习帖---http://bbs.youkuaiyun.com/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包 官网有下载) 
  同样以一个实体类为例讲述---
       简单用个实体类吧--
Java code
?
1
2
3
4
5
6
  public  class  Student(){
       private  String sno; //学号
       private  Stirng sname; //姓名
       private  String sex; //性别
      /************相关get,set方法省略********/
  }

   这是基于注解spring配置
 
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 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 这个文件名字定义 内容如下:-->

Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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/schema/context/spring-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接口类实现细节(就以插入记录为例):
 
Java code
?
1
2
3
4
  public  interface  StudentDao{
      @Insert ( "insert into student(sno,sname,sex) values(#{sno},#{sname},#{sex})" )
      public  boolean  addStudentInfos(Student student);
}


Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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/schema/context/spring-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>classpath:DataSource.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框架完成***********************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值