赞
踩
1.pojo中的User类
package com.ysj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String password;
}
2.创建Mpper包中的Mapper
package com.ysj.mapper;
import com.ysj.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUsers();
}
3.在2创建到之后在资源包中创建一个同名同包的UserMapper.xml用来写sql语句 mapmper的命名空间是本类的全称
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ysj.mapper.UserMapper">
<select id="getUsers" resultType="user">
select *
from user;
</select>
</mapper>
4.接口类的实现
package com.ysj.mapper; import com.ysj.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperImpl implements UserMapper{ //我们的所有的操作,都使用sqlSession来执行,在原来。而现在都使用sqlSessionTemplate; private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<User> getUsers() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.getUsers(); } }
5.配置文件的配置
<?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核心配置文件--> <configuration> <settings> <!--标准的日志工厂实现--> <setting name="logImpl" value="STDOUT_LOGGING"/> <!--是否开启自动驼峰命名规则(camel case)映射--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--显示的开启默认缓存--> <setting name="cacheEnabled" value="true"/> </settings> <typeAliases> <!--别用小写--> <package name="com.ysj.pojo"/> </typeAliases> </configuration>
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--专注操作数据库 很少改内容--> <!--DataSource:使用Spring-的数据源替换Mybatis的配置--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="88888888"/> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--绑定mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--mapper的注册--> <property name="mapperLocations" value="classpath:com/ysj/mapper/UserMapper.xml"/> <!--别名的使用--> <property name="typeAliasesPackage" value="com.ysj.pojo"/> </bean> <!--SqlSessionTemplate:就是我们使用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!--只能使用构造器注入sqlSessionFactory,因为没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--实现类的实例化,以及引用其他包,之后有后续直接引进本文件--> <import resource="spring-dao.xml"/> <bean id="userMapper" class="com.ysj.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean> </beans>
6.test
package org.example; import com.ysj.mapper.UserMapper; import com.ysj.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; import java.util.List; /** * Unit test for simple App. */ public class AppTest { @Test public void test1() throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> users = userMapper.getUsers(); for (User user : users) { System.out.println(user); } } }
注意事项(理解):
public void testAddUser() {
//获得配置文件的信息
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//利用反射得到UserMapperImpl的对象
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
//反射得到的对象 调用方法
userMapper.addUser(new User(7, "王小帅", "ai1234"));
}
public void testDeleteUser() {
}
@Override
public int addUser(User user) {
//sqlSession已经注入 得到UserMapper的代理
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//代理对象在进行操作xml中的addUser
return mapper.addUser(user);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。