1. 回忆mybatis

实体类pojo

  1. package cn.edu.jxust.pojo;
  2. import lombok.Data;
  3. import java.sql.Date;
  4. @Data
  5. public class User {
  6. private int id;
  7. private String stuName;
  8. private String age;
  9. private Date brithday;
  10. }

数据访问层mapper

  1. package cn.edu.jxust.mapper;
  2. import cn.edu.jxust.pojo.User;
  3. import java.util.List;
  4. public interface UserMapper {
  5. public List<User> selectUser();
  6. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="cn.edu.jxust.mapper.UserMapper">
  6. <select id="selectUser" resultType="user">
  7. select * from t_stu;
  8. </select>
  9. </mapper>

XML

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings>
  9. <typeAliases>
  10. <package name="cn.edu.jxust.pojo"/>
  11. </typeAliases>
  12. <environments default="development">
  13. <environment id="development">
  14. <transactionManager type="JDBC"/>
  15. <dataSource type="POOLED">
  16. <property name="driver" value="com.mysql.jdbc.Driver"/>
  17. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"/>
  18. <property name="username" value="root"/>
  19. <property name="password" value="abc@123"/>
  20. </dataSource>
  21. </environment>
  22. </environments>
  23. <mappers>
  24. <mapper class="cn.edu.jxust.mapper.UserMapper"/>
  25. </mappers>
  26. </configuration>

测试Test

  1. import cn.edu.jxust.mapper.UserMapper;
  2. import cn.edu.jxust.pojo.User;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.List;
  10. public class Test {
  11. @org.junit.Test
  12. public void test() throws IOException {
  13. String resources = "mybatis-config.xml";
  14. InputStream in = Resources.getResourceAsStream(resources);
  15. System.out.println(in);
  16. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
  17. SqlSession sqlSession = sessionFactory.openSession(true);
  18. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  19. List<User> userList = mapper.selectUser();
  20. for(User user : userList){
  21. System.out.println(user);
  22. }
  23. }
  24. }

2. Mybatis-Spring

  1. 编写数据源配置
  2. sqlSessionFactory
  3. sqlSessionTemplate
  4. 需要给接口加实现类【】
  5. 将自己写的实现类,注入到spring中

1)实体类pojo

  1. package cn.edu.jxust.pojo;
  2. import lombok.Data;
  3. import java.sql.Date;
  4. @Data
  5. public class User {
  6. private int id;
  7. private String stuName;
  8. private String age;
  9. private Date brithday;
  10. }

2)数据访问层mapper

  1. package cn.edu.jxust.mapper;
  2. import cn.edu.jxust.pojo.User;
  3. import java.util.List;
  4. public interface UserMapper {
  5. public List<User> selectUser();
  6. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="cn.edu.jxust.mapper.UserMapper">
  6. <select id="selectUser" resultType="user">
  7. select * from t_stu;
  8. </select>
  9. </mapper>

3)资源类resources

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings>
  9. <typeAliases>
  10. <package name="cn.edu.jxust.pojo"/>
  11. </typeAliases>
  12. </configuration>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/aop
  11. https://www.springframework.org/schema/aop/spring-aop.xsd">
  12. <!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 ddbcp druidi
  13. 我们这里使用Spring提供的JDBC:org.springframwork.jdbc.datasource
  14. -->
  15. <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  16. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  17. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"/>
  18. <property name="username" value="root"/>
  19. <property name="password" value="abc@123"/>
  20. </bean>
  21. <!--sqlSessionFactory-->
  22. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  23. <property name="dataSource" ref="datasource" />
  24. <!--绑定myabtis配置文件-->
  25. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  26. <property name="mapperLocations" value="classpath:cn/edu/jxust/mapper/*.xml"/>
  27. </bean>
  28. <!--SqlSessionTemplate:就是我们使用的sqlSession-->
  29. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  30. <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
  31. <constructor-arg index="0" ref="sqlSessionFactory"/>
  32. </bean>
  33. <bean id="userMapper" class="cn.edu.jxust.mapper.UserMapperImpl">
  34. <property name="sqlSession" ref="sqlSession"/>
  35. </bean>
  36. </beans>

4)测试类Test

  1. import cn.edu.jxust.mapper.UserMapper;
  2. import cn.edu.jxust.pojo.User;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import java.io.IOException;
  6. public class Test {
  7. @org.junit.Test
  8. public void test() throws IOException {
  9. ApplicationContext context = new ClassPathXmlApplicationContext("spirngdao.xml");
  10. UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
  11. for (User user:userMapper.selectUser()){
  12. System.out.println(user);
  13. }
  14. }
  15. }