首页 技术 正文
技术 2022年11月20日
0 收藏 788 点赞 2,841 浏览 3401 个字

初学MyBatis-Spring,发个帖子记录一下自己的学习历程,先写个自己的编程步骤。

1.先进行数据库设计。

2.根据所设计的数据,写出相应的POJO,并写出setter和getter方法。

3.编写Mapper接口,并写出其相应的xml文件。

4.编写MyBatisConfig.xml文件,现在看来只需要编写其setting。

5.开始编写springbeans.xml文件。

(1)

记得先编写dataSource bean 该bean的class选择为:org.springframework.jdbc.datasource.SimpleDriverDataSource

dataSource bean的四个参数driverClass、url、username、password可以直接赋值,也可以从properties文件中引用。

那么先创建一个dataSource.properties文件,如下:

MyBatis-Spring开发记录贴(新手)

然后在springbeans.xml文件中引用这个properties文件,引用代码如下:

<context:property-placeholder ignore-resource-not-found="false" location="classpath:dataSource.properties"/>

  当然,在使用context时要记得添加它的命名空间,否则将会报错,如下加粗的三行代码:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
">

这样,就可以调用dataSource.properties文件中的数据了。

如下,用调用的方法给dataSource  bean中的参数赋值:

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.database.driver}" />
<property name="url" value="${jdbc.database.url}" />
<property name="username" value="${jdbc.database.username}" />
<property name="password" value="${jdbc.database.password}" />
</bean>

(2)

接下来应该配置SqlSessionFactory Bean,我们都明白SqlSessionFactory是产生SqlSession的基础,在MyBatis中是使用SqlSessionFactoryBuilder来创建SqlSessionFactory,而在MyBatis+Spring中,提供了SqlSessionFactoryBean去支持SqlSessionFactory的配置。配置方法如下:

<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:MyBatisConfig.xml"/>
</bean>

其中 dataSource就是前面我们所创建的dataSource Bean,configLocation 是MyBatis配置文件的定位。

(3)

接下来配置MapperFactoryBean,如下所示:

<bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.UserMapper"/>
<property name="SqlSessionFactory" ref="SqlSessionFactory"/>
</bean>

其中,参数mapperInterface的value值为该映射器接口的路径,SqlSessionFactory就是上一步所配置的SqlSessionFactory Bean。

当然,这是引入一个Mapper的配制方法,当需要配置很多mapper时,一个一个的进行配置会耗费很多时间,这是可以使用MapperScannerConfigurer进行配置,但是暂时还没有学习配置的方法,这个内容就先不讲。应该很简单,不难。

6.接下来就可以编写一个Main进行测试了,如下所示:

package doing;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import mapper.CreateTableMapper;
import mapper.FileNameMapper;
import mapper.UserMapper;
import pojo.FileName;
import pojo.User;
public class Main {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringBeans.xml"); UserMapper userMapper = ctx.getBean(UserMapper.class);
User user = new User();
/*user = userMapper.getUserById(3L);
System.out.println(user.getFiles().get(1).getFile_name());
FileNameMapper fileNameMapper = ctx.getBean(FileNameMapper.class);
fileNameMapper.deleteFileNameByUser_Name(user.getUser_name());
userMapper.deleteUserById(3L);*/ user.setUser_name("zhouchunjie");
user.setPassword("12345");
user.setMobile("17721260015");
user.setNote("No Note");
userMapper.insertUser(user);
}
}

那么MyBatis+Spring的编程步骤大概就是这样,对于我这个菜鸟来说,有一个自己的编程步骤能让我敲代码时思维不那么混乱。

在进行MyBatis+Spring编程时,千万要记得导入mybatis-spring-1.3.0.jar!!!

在本次编程的过程中,还发现,MyBatis不仅仅可以对已经存在的表进行查询,删除,更新等等等等操作,而且还可以创建新的表单!具体方法懒得码字了。。。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894