mybatis--Spring整合mybatis
今天学习了mybatis整合Spring开发,做了一个mybatis+spring的小实例
(1)首先,创建数据库my,并在数据库my中创建表user
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
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
package com.zk.dao; import java.util.List; import com.zk.pojo.User; public interface UserMapper { List<User> getUser(User user); }
UserMapperImpl.java
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
<?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
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
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
<?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
<?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
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); } }
运行结果图如下:
程序结构图如下: