mybatis--Spring整合mybatis
今天学习了mybatis整合Spring开发,做了一个mybatis+spring的小实例
(1)首先,创建数据库my,并在数据库my中创建表user
1 2 3 4 5 6 7 8 9 10 11 12 | create database my; use my; create table user( id int ( 10 ) auto_increment, name varchar( 64 ), level varchar( 256 ), phone varchar( 256 ), primary key(id) ); insert into user(id,name,level,phone) values( 1 , 'a' , 'a' , '1234555666' ); |
(2)导入jar包
(3)在com.zk.pojo下创建实体类User.java
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 | package com.zk.pojo; public class User { private int id; private String name; private String level; private String phone; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getLevel() { return level; } public void setLevel(String level) { this .level = level; } public String getPhone() { return phone; } public void setPhone(String phone) { this .phone = phone; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", level=" + level + ", phone=" + phone + "]" ; } } |
(4)创建dao层接口、实现类、mapper映射文件(UserMapper.java、UserMapperImpl.java、UserMapper.xml)
UserMapper.java
1 2 3 4 5 6 7 8 9 10 | package com.zk.dao; import java.util.List; import com.zk.pojo.User; public interface UserMapper { List<User> getUser(User user); } |
UserMapperImpl.java
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 | package com.zk.dao; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import com.zk.pojo.User; public class UserMapperImpl implements UserMapper{ public SqlSessionTemplate sqlSession; @Override public List<User> getUser(User user) { // TODO Auto-generated method stub return sqlSession.selectList( "com.zk.dao.UserMapper.getUserList" ,user); } public SqlSessionTemplate getSqlSession() { return sqlSession; } public void setSqlSession(SqlSessionTemplate sqlSession) { this .sqlSession = sqlSession; } } |
UserMapper.xml
1 2 3 4 5 6 7 8 9 | <?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= "com.zk.dao.UserMapper" > <select id= "getUserList" resultType= "User" parameterType= "User" > SELECT * FROM `user` </select> </mapper> |
(5)创建service层接口、实现类(UserService.java、UserServiceImpl.java)
UserService.java
1 2 3 4 5 6 7 8 9 10 | package com.zk.service; import java.util.List; import com.zk.pojo.User; public interface UserService{ List<User> findUserList(User user) throws Exception; } |
UserServiceImpl.java
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 | package com.zk.service; import java.util.List; import com.zk.dao.UserMapper; import com.zk.pojo.User; public class UserServiceImpl implements UserService{ private UserMapper usermapper; @Override public List<User> findUserList(User user) throws Exception { // TODO Auto-generated method stub try { return usermapper.getUser(user); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } } public UserMapper getUsermapper() { return usermapper; } public void setUsermapper(UserMapper usermapper) { this .usermapper = usermapper; } } |
(6)配置config.xml
1 2 3 4 5 6 7 8 9 | <?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> <typeAliases> < package name= "com.zk.pojo" /> </typeAliases> </configuration> |
(7)配置ApplicationContext.xml
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 | <?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:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 配置数据源 --> <bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" > <property name= "driverClassName" value= "com.mysql.jdbc.Driver" /> <property name= "url" value= "jdbc:mysql://localhost:3306/my" /> <property name= "username" value= "root" /> <property name= "password" value= "123456" /> </bean> <!-- 获得sqlSessionFactory --> <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <!-- 映射数据源 --> <property name= "dataSource" ref= "dataSource" /> <!-- 映射mybatis核心配置文件 --> <property name= "configLocation" value= "config/config.xml" /> <!-- 映射mapper文件 --> <property name= "mapperLocations" > <list> <value>classpath:com/zk/dao /**/ *.xml</value> </list> </property> </bean> <!-- 获得sqlSession --> <bean id= "sqlSession" class = "org.mybatis.spring.SqlSessionTemplate" > <constructor-arg name= "sqlSessionFactory" ref= "sqlSessionFactory" /> </bean> <bean id= "UserMapper" class = "com.zk.dao.UserMapperImpl" > <property name= "sqlSession" ref= "sqlSession" /> </bean> <bean id= "UserService" class = "com.zk.service.UserServiceImpl" > <property name= "usermapper" ref= "UserMapper" /> </bean> </beans> |
(8)测试类Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package test; import java.util.ArrayList; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zk.pojo.User; import com.zk.service.UserService; public class test { public static void main(String[]args) throws Exception { ApplicationContext ac= new ClassPathXmlApplicationContext( "config/applicationContext.xml" ); UserService us=(UserService) ac.getBean( "UserService" ); List<User> user1= new ArrayList<User>(); User user= new User(); user1=us.findUserList(user); System.out.println(user1); } } |
运行结果图如下:
程序结构图如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)