整合Mybatis
步骤:
- 导包
- junit
- mysql
- mybatis
- mybatis-spring
- spring-webMVC
- 编写配置文件
- 测试
回忆mybatis
-
编写实体类
public class User { private int id; private String name; private int age; private String email; public User() { } public User(int id, String name, int age, String email) { this.id = id; this.name = name; this.age = age; this.email = email; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", email='" + email + '\'' + '}'; } }
-
编写核心配置文件
<?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> <typeAliases> <package name="com.wjq.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&useSSL=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.wjq.mapper.UserMapper"/> </mappers> </configuration>
-
编写接口
public interface UserMapper { public List<User> queryUser(); }
-
编写Mapper.xml
<?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.wjq.mapper.UserMapper"> <select id="queryUser" resultType="user"> select * from user </select> </mapper>
-
测试
public class MyTest { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = factory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.queryUser(); for (User user : users) { System.out.println(user); } sqlSession.close(); } }
整合Mybatis
-
编写spring-dao.xml获取数据源,获取SqlSessionFactory(从SqlSessionFactoryBean中获得),获取SqlSession(从SqlSessionTemplate中获得)
<?xml version="1.0" encoding="UTF-8"?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 获取数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="password" value="123456"/> <property name="username" value="root"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&useSSL=true"/> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> </bean> <!-- 获取sqlSessionFactory通过SqlSessionFactoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 用到数据源--> <property name="dataSource" ref="dataSource"/> <!-- 绑定Mybatis配置文件,之前写过,删除environments标签--> <property name="configLocation" value="mybatis-config.xml"/> <!-- <property name="mapperLocations" value="classpath:com/wjq/mapper/*.xml"/>--> </bean> <!--SqlSessionTemplate 就是我们所用到的SqlSession 参数是SqlSessionFactory类型,public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory){} 并且没有set()--> <!--方式二可以不用这步因为SqlSessionDaoSupport类里面会创建SqlSessionTemplate--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
-
编写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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <bean id="userMapper" class="com.wjq.mapper.UserMapperImpl"> <constructor-arg index="0" ref="sqlSession"/> </bean> <!--方式二的配置文件--> <!-- 因为继承的类的set()的参数是SqlSessionFactory类型--> <!--<bean id="userMapper2" class="com.wjq.mapper.UserMapperImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>--> </beans>
-
接口
public interface UserMapper { public List<User> queryUser(); }
-
实现类
方式一:
public class UserMapperImpl implements UserMapper { //获得sqlSession private SqlSessionTemplate sqlSession; public UserMapperImpl(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } public List<User> queryUser() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.queryUser(); } }
方式二:
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper { public List<User> queryUser() { return getSqlSession().getMapper(UserMapper.class).queryUser(); } }
-
测试
public class MyTest {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
List<User> users = userMapper.queryUser();
for (User user : users) {
System.out.println(user);
}
}
}