Spring整合mybatis
Spring集成mybatis
一、创建实体类
package com.wuxin.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
二、创建接口
package com.wuxin.mapper;
import com.wuxin.pojo.User;
import java.util.List;
import java.util.Map;
public interface UserMapper {
public int addUser(User user);
public int delUser(int id);
public int upUser(Map map);
public List<User>selectAll();
}
三、创建接口对应的实现类
package com.wuxin.mapper;
import com.wuxin.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
import java.util.Map;
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
public int addUser(User user) {
return getSqlSession().getMapper(UserMapper.class).addUser(user);
}
public int delUser(int id) {
return getSqlSession().getMapper(UserMapper.class).delUser(id);
}
public int upUser(Map map) {
return getSqlSession().getMapper(UserMapper.class).upUser(map);
}
public List<User> selectAll() {
return getSqlSession().getMapper(UserMapper.class).selectAll();
}
}
四、创建接口对应的xml文件
<?xml version="1.0" encoding="gbK" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="com.wuxin.mapper.UserMapper">
<insert id="addUser" parameterType="user">
insert into mybatis.user(id, name, pwd)
values (#{id},#{name},#{pwd});
</insert>
<delete id="delUser">
delete from mybatis.user
where id = #{id}
</delete>
<update id="upUser" parameterType="user">
update mybatis.user
set id = #{id},name=#{name},pwd=#{pwd}
where id = #{id};
</update>
<select id="selectAll" resultType="user">
select * from mybatis.user
</select>
</mapper>
五、创建mybatis-config.xml、spring-dao.xml、applicationContext.xml配置文件
<?xml version="1.0" encoding="gbK" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration>
<typeAliases>
<package name="com.wuxin.pojo"/>
</typeAliases>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--DataSource配置数据库连接-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123123"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--Mybatis配置文件-->
<property name="configLocation" value="mybatis-config.xml"/>
<!--解析xml-->
<property name="mapperLocations" value="classpath:com/wuxin/mapper/*.xml"/>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.wuxin.mapper.UserMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
六、创建测试类
import com.wuxin.mapper.UserMapper;
import com.wuxin.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.HashMap;
import java.util.Map;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
User user1 = new User();
user1.setId(5);
user1.setName("五星");
user1.setPwd("666665");
userMapper.addUser(user1);
for (User user : userMapper.selectAll()) {
System.out.println(user);
}
userMapper.delUser(5);
for (User user : userMapper.selectAll()) {
System.out.println(user);
}
Map map = new HashMap();
map.put("id",4);
map.put("pwd","454568");
userMapper.upUser(map);
for (User user : userMapper.selectAll()) {
System.out.println(user);
}
}
}
苟利国家生死以,岂因祸福避趋之