Spring集成MyBatis

news/2024/4/29 15:56:37

基本准备

创建Dynamic Web Project
引入相关jar包

        Spring框架相关jar包

        MyBatis连接Spring相关jar包

        连接MySQL驱动包

         JSTL标签库包

添加db.properties文件,该属性文件配置连接数据库相关信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
user=root
password=
添加Spring配置文件,在src中新增applicationContext.xml,并引入该属性文件
<!-- 导入数据库连接信息的属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
在Spring配置文件中添加DataSource的配置,PooledDataSource为MyBatis实现的数据库连接池 
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver" value="${driver}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="url" value="${url}"></property>
</bean>
在Spring配置文件中添加SqlSessionFactoryBean,用来创建SqlSessionFactory对象

        configLocation:用于指定MyBatis的mybatis.xml配置文件的路径

         dataSource:用于配置数据源,该属性为必选项,可以直接引用已经配置好的dataSource数据库连接池

        mapperLocations:扫描XML映射文件的路径 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 添加mybatis主配置文件的位置 --><property name="configLocation" value="classpath:mybatis.xml"/><!-- 需要一个数据源 --><property name="dataSource" ref="dataSource"></property><!-- 设置映射文件的位置 --><property name="mapperLocations"><array><value>classpath:net/onest/server/dao/*.xml</value></array></property>
</bean>
在Spring配置文件中添加MapperScannerConfigurer,自动扫描所有的Mapper接口

basePackage:用于配置基本的包路径 

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="net.onest.server.dao"/>
</bean>
按照SSH集成的web.xml,添加Spring和SpringMVC相关配置
创建实体类
创建数据库表
创建index.jsp 

 

开发Mapper层(Dao层)

Mapper层也就是常说的数据访问层(Dao层)。根据配置文件中配置的自动扫描接口的包名创建映射接口和XML映射文件

public interface UserMapper {public List<User> findAllUsers();public int saveUser(User u);
}
<mapper namespace="net.onest.server.dao.UserMapper"><resultMap type="net.onest.server.entity.User" id="userMap"><id column="id" property="id"/><result column="user_name" property="userName"/><result column="password" property="password"/></resultMap><select id="findAllUsers" resultMap="userMap">select * from user</select><insert id="saveUser">insert into user(user_name,password)  values(#{userName},#{password})</insert>
</mapper>

开发业务层(Service层)

添加Service层的接口和实现类

public interface UserService {public List<User> findAllUsers();public int saveUser(User u);
}
@Service
public class UserServiceImpl implements UserService{@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> findAllUsers() {return userMapper.findAllUsers();}@Overridepublic int saveUser(User u) {return userMapper.saveUser(u);}
}

Service的实现类需要添加@Service注解,由于在Spring配置文件中配置了自动扫描Service实现类所在的包,所以Spring在初始化时就会扫描到添加了@Service注解的类

由于配置了自动扫描Mapper接口,所以在Service层可以使用@AutoWired注解自动注入Mapper 

 

开发控制层(Controller层)

@RequestMapping("/user")
@Controller
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/userList")public ModelAndView getUsers() {ModelAndView mv = new ModelAndView("userList");List<User> users = userService.findAllUsers();mv.addObject("users", users);return mv;}……
}
@RequestMapping("/addUser")
public ModelAndView addUser() {ModelAndView mv = new ModelAndView("addUser");User u = new User();mv.addObject("user", u);return mv;
}
@RequestMapping("/saveUser")
public ModelAndView saveUser(User u) {ModelAndView mv = new ModelAndView();userService.saveUser(u);mv.setViewName("redirect:/user/userList");return mv;
}

 

开发视图层(View层)

根据在SpringMVC配置文件中的视图配置需要在WebContent/WEB-INF中新建jsp目录来存放所有的jsp文件

<bean id="viewResolver" class=
"org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/>
</bean>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/11710.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Golang Gin框架

1、这篇文章我们简要讨论一些Gin框架 主要是给大家一个基本概念 1、Gin主要是分为路由和中间件部分。 Gin底层使用的是net/http的逻辑&#xff0c;net/http主要是说&#xff0c;当来一个网络请求时&#xff0c;go func开启另一个协程去处理后续(类似epoll)。 然后主协程持续…

大数据学习十三天(hadhoop基础2)

一: MapReduce概述(了解) MapReduce是hadoop三大组件之一,是分布式计算组件 Map阶段 : 将数据拆分到不同的服务器后执行Maptask任务,得到一个中间结果 Reduce阶段 : 将Maptask执行的结果进行汇总,按照Reducetask的计算 规则获得一个唯一的结果 我们在MapReduce计算框架的使用过…

微信小程序实现左滑删除

效果 实现思路 使用的是官方提供的movable-area 嵌套movable-view 1、movable-area&#xff1a;注意点&#xff0c;需要设置其高度&#xff0c;否则会出现列表内容重叠的现象。 2、由于movable-view需要向右移动&#xff0c;左滑的时候给删除控件展示的空间&#xff0c;故 mov…

2024免费Mac苹果解压压缩包软件BetterZip5

在2024年&#xff0c;对于Mac电脑用户来说&#xff0c;如果你想要无需解压就能快速查看压缩文档的内容&#xff0c;BetterZip是一个极佳的选择。这款软件不仅支持多种格式的压缩和解压&#xff0c;如zip、rar、7z、tar等&#xff0c;还具备丰富的功能和设置&#xff0c;包括预览…

备战蓝桥杯Day37 - 真题 - 特殊日期

一、题目描述 思路&#xff1a; 1、统计2000年到2000000年的日期&#xff0c;肯定是需要遍历 2、闰年的2月是29天&#xff0c;非闰年的2月是28天。我们需要判断这一年是否是闰年。 1、3、5、7、8、10、12月是31天&#xff0c;4、6、9、11月是30天。 3、年份yy是月份mm的倍数…

论文阅读AI工具链

文献检索 可以利用智谱清言来生成合适的文献检索式&#xff0c;并根据需要不断调整。 谷歌学术 在Google Scholar中进行检索时&#xff0c;您可以使用类似的逻辑来构建您的搜索式&#xff0c;但是语法会有所不同。Google Scholar的搜索框接受普通的文本搜索&#xff0c;但是…