Mybatis通过parameterType来设置插入的信息类型-----Mybatis框架

悠悠 2024-03-08 08:55 46阅读 0赞
  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. <properties resource="jdbc.properties"/>
  7. <settings>
  8. <setting name="logImpl" value="SLF4J"/>
  9. </settings>
  10. <typeAliases>
  11. <package name="com.powernode.mybatis.POJO"/>
  12. </typeAliases>
  13. <environments default="development">
  14. <environment id="development">
  15. <transactionManager type="JDBC"/>
  16. <dataSource type="POOLED">
  17. <property name="driver" value="${jdbc.driver}"/>
  18. <property name="url" value="${jdbc.url}"/>
  19. <property name="username" value="${jdbc.username}"/>
  20. <property name="password" value="${jdbc.password}"/>
  21. </dataSource>
  22. </environment>
  23. </environments>
  24. <mappers>
  25. <package name="com.powernode.mybatis.Mapper"/>
  26. </mappers>
  27. </configuration>
  28. <?xml version="1.0" encoding="UTF-8" ?>
  29. <!DOCTYPE configuration
  30. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  31. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  32. <configuration>
  33. <properties resource="jdbc.properties"/>
  34. <settings>
  35. <setting name="logImpl" value="SLF4J"/>
  36. </settings>
  37. <typeAliases>
  38. <package name="com.powernode.mybatis.POJO"/>
  39. </typeAliases>
  40. <environments default="development">
  41. <environment id="development">
  42. <transactionManager type="JDBC"/>
  43. <dataSource type="POOLED">
  44. <property name="driver" value="${jdbc.driver}"/>
  45. <property name="url" value="${jdbc.url}"/>
  46. <property name="username" value="${jdbc.username}"/>
  47. <property name="password" value="${jdbc.password}"/>
  48. </dataSource>
  49. </environment>
  50. </environments>
  51. <mappers>
  52. <package name="com.powernode.mybatis.Mapper"/>
  53. </mappers>
  54. </configuration>
  55. <?xml version="1.0" encoding="UTF-8"?>
  56. <!DOCTYPE mapper
  57. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  58. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  59. <mapper namespace="com.powernode.mybatis.Mapper.StudentMapper">
  60. <!-- parameterType的作用是告诉Mybatis框架我们的属性是什么类型,但是大部分情况下自动推断后我们就不用写了-->
  61. <!-- parameterType其实可以写别名,Mybatis内置了很多别名-->
  62. <select id="selectById" resultType="Student" parameterType="long">
  63. select * from t_student where id = #{id,javaType=Long,jdbcType=BIGINT};
  64. </select>
  65. <select id="selectByName" resultType="Student" parameterType="String">
  66. select * from t_student where name = #{name,javaType=String,jdbcType=VARCHAR};
  67. </select>
  68. <select id="selectByBirth" resultType="Student" parameterType="Date">
  69. select * from t_student where birth = #{birth,javaType=Date,jdbcType=DATE};
  70. </select>
  71. <select id="selectBySex" resultType="Student" parameterType="Character">
  72. select * from t_student where sex = #{sex,javaType=Character,jdbcType=CHAR}
  73. </select>
  74. <insert id="insertStudentsByMap">
  75. insert into t_student(id,name,age,sex,birth,height) values(null,#{姓名},#{年龄},#{性别},#{生日},#{身高});
  76. </insert>
  77. <insert id="InsertStudentByPOJO" parameterType="com.powernode.mybatis.POJO.Student">
  78. insert into t_student(id,name,age,sex,birth,height) values(null,#{name},#{age},#{sex},#{birth},#{height});
  79. </insert>
  80. </mapper>
  81. <?xml version="1.0" encoding="UTF-8"?>
  82. <!DOCTYPE mapper
  83. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  84. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  85. <mapper namespace="com.powernode.mybatis.Mapper.StudentMapper">
  86. <!-- parameterType的作用是告诉Mybatis框架我们的属性是什么类型,但是大部分情况下自动推断后我们就不用写了-->
  87. <!-- parameterType其实可以写别名,Mybatis内置了很多别名-->
  88. <select id="selectById" resultType="Student" parameterType="long">
  89. select * from t_student where id = #{id,javaType=Long,jdbcType=BIGINT};
  90. </select>
  91. <select id="selectByName" resultType="Student" parameterType="String">
  92. select * from t_student where name = #{name,javaType=String,jdbcType=VARCHAR};
  93. </select>
  94. <select id="selectByBirth" resultType="Student" parameterType="Date">
  95. select * from t_student where birth = #{birth,javaType=Date,jdbcType=DATE};
  96. </select>
  97. <select id="selectBySex" resultType="Student" parameterType="Character">
  98. select * from t_student where sex = #{sex,javaType=Character,jdbcType=CHAR}
  99. </select>
  100. <insert id="insertStudentsByMap">
  101. insert into t_student(id,name,age,sex,birth,height) values(null,#{姓名},#{年龄},#{性别},#{生日},#{身高});
  102. </insert>
  103. <insert id="InsertStudentByPOJO" parameterType="com.powernode.mybatis.POJO.Student">
  104. insert into t_student(id,name,age,sex,birth,height) values(null,#{name},#{age},#{sex},#{birth},#{height});
  105. </insert>
  106. </mapper>
  107. package com.powernode.mybatis.Mapper;
  108. import com.powernode.mybatis.POJO.Student;
  109. import java.util.Date;
  110. import java.util.List;
  111. import java.util.Map;
  112. import java.util.Objects;
  113. public interface StudentMapper
  114. {
  115. //通过POJO保存学生信息
  116. int InsertStudentByPOJO(Student student);
  117. //通过Map保存学生信息
  118. int insertStudentsByMap(Map<String, Object> map);
  119. List<Student> selectById(Long id);
  120. List<Student> selectByName(String name);
  121. List<Student> selectByBirth(Date birth);
  122. List<Student> selectBySex(Character sex);
  123. }
  124. package com.powernode.mybatis.Mapper;
  125. import com.powernode.mybatis.POJO.Student;
  126. import java.util.Date;
  127. import java.util.List;
  128. import java.util.Map;
  129. import java.util.Objects;
  130. public interface StudentMapper
  131. {
  132. //通过POJO保存学生信息
  133. int InsertStudentByPOJO(Student student);
  134. //通过Map保存学生信息
  135. int insertStudentsByMap(Map<String, Object> map);
  136. List<Student> selectById(Long id);
  137. List<Student> selectByName(String name);
  138. List<Student> selectByBirth(Date birth);
  139. List<Student> selectBySex(Character sex);
  140. }
  141. package com.powernode.mybatis.Test;
  142. import com.powernode.mybatis.Mapper.StudentMapper;
  143. import com.powernode.mybatis.POJO.Student;
  144. import com.powernode.mybatis.Utils.SqlSessionUtil;
  145. import org.apache.ibatis.session.SqlSession;
  146. import java.text.SimpleDateFormat;
  147. import java.util.Date;
  148. import java.util.HashMap;
  149. import java.util.List;
  150. import java.util.Map;
  151. public class Test
  152. {
  153. @org.junit.Test
  154. public void Test()
  155. {
  156. SqlSession sqlSession = SqlSessionUtil.openSession();
  157. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  158. List<Student> students = mapper.selectById(1L);
  159. students.forEach(student -> {
  160. System.out.println(student);
  161. });
  162. SqlSessionUtil.close(sqlSession);
  163. }
  164. @org.junit.Test
  165. public void TestSelect()
  166. {
  167. SqlSession sqlSession = SqlSessionUtil.openSession();
  168. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  169. List<Student> list = mapper.selectByName("李四");
  170. list.forEach(l ->{
  171. System.out.println(l);
  172. });
  173. }
  174. @org.junit.Test
  175. public void TestDate() throws Exception
  176. {
  177. SqlSession sqlSession = SqlSessionUtil.openSession();
  178. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  179. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  180. Date birth = simpleDateFormat.parse("2000-10-11");
  181. List<Student> list = mapper.selectByBirth(birth);
  182. list.forEach(l -> {
  183. System.out.println(l);
  184. });
  185. }
  186. @org.junit.Test
  187. public void TestSEX()
  188. {
  189. SqlSession sqlSession = SqlSessionUtil.openSession();
  190. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  191. List<Student> list = mapper.selectBySex('男');
  192. list.forEach(te -> {
  193. System.out.println(te);
  194. });
  195. }
  196. @org.junit.Test
  197. public void TestInsertMap() throws Exception
  198. {
  199. SqlSession sqlSession = SqlSessionUtil.openSession();
  200. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  201. Map<String,Object> map = new HashMap<>();
  202. map.put("姓名","王五");
  203. map.put("年龄",20);
  204. map.put("身高",1.82);
  205. map.put("性别",'男');
  206. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  207. Date date = simpleDateFormat.parse("2001-08-14");
  208. map.put("生日",date);
  209. mapper.insertStudentsByMap(map);
  210. sqlSession.commit();
  211. SqlSessionUtil.close(sqlSession);
  212. }
  213. @org.junit.Test
  214. public void TestInsertByPOJO()
  215. {
  216. SqlSession sqlSession = SqlSessionUtil.openSession();
  217. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  218. Student student = new Student(null,"展会",20,1.8,new Date(),'女');
  219. int i = mapper.InsertStudentByPOJO(student);
  220. System.out.println(i);
  221. sqlSession.commit();
  222. SqlSessionUtil.close(sqlSession);
  223. }
  224. }
  225. package com.powernode.mybatis.Test;
  226. import com.powernode.mybatis.Mapper.StudentMapper;
  227. import com.powernode.mybatis.POJO.Student;
  228. import com.powernode.mybatis.Utils.SqlSessionUtil;
  229. import org.apache.ibatis.session.SqlSession;
  230. import java.text.SimpleDateFormat;
  231. import java.util.Date;
  232. import java.util.HashMap;
  233. import java.util.List;
  234. import java.util.Map;
  235. public class Test
  236. {
  237. @org.junit.Test
  238. public void Test()
  239. {
  240. SqlSession sqlSession = SqlSessionUtil.openSession();
  241. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  242. List<Student> students = mapper.selectById(1L);
  243. students.forEach(student -> {
  244. System.out.println(student);
  245. });
  246. SqlSessionUtil.close(sqlSession);
  247. }
  248. @org.junit.Test
  249. public void TestSelect()
  250. {
  251. SqlSession sqlSession = SqlSessionUtil.openSession();
  252. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  253. List<Student> list = mapper.selectByName("李四");
  254. list.forEach(l ->{
  255. System.out.println(l);
  256. });
  257. }
  258. @org.junit.Test
  259. public void TestDate() throws Exception
  260. {
  261. SqlSession sqlSession = SqlSessionUtil.openSession();
  262. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  263. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  264. Date birth = simpleDateFormat.parse("2000-10-11");
  265. List<Student> list = mapper.selectByBirth(birth);
  266. list.forEach(l -> {
  267. System.out.println(l);
  268. });
  269. }
  270. @org.junit.Test
  271. public void TestSEX()
  272. {
  273. SqlSession sqlSession = SqlSessionUtil.openSession();
  274. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  275. List<Student> list = mapper.selectBySex('男');
  276. list.forEach(te -> {
  277. System.out.println(te);
  278. });
  279. }
  280. @org.junit.Test
  281. public void TestInsertMap() throws Exception
  282. {
  283. SqlSession sqlSession = SqlSessionUtil.openSession();
  284. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  285. Map<String,Object> map = new HashMap<>();
  286. map.put("姓名","王五");
  287. map.put("年龄",20);
  288. map.put("身高",1.82);
  289. map.put("性别",'男');
  290. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  291. Date date = simpleDateFormat.parse("2001-08-14");
  292. map.put("生日",date);
  293. mapper.insertStudentsByMap(map);
  294. sqlSession.commit();
  295. SqlSessionUtil.close(sqlSession);
  296. }
  297. @org.junit.Test
  298. public void TestInsertByPOJO()
  299. {
  300. SqlSession sqlSession = SqlSessionUtil.openSession();
  301. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
  302. Student student = new Student(null,"展会",20,1.8,new Date(),'女');
  303. int i = mapper.InsertStudentByPOJO(student);
  304. System.out.println(i);
  305. sqlSession.commit();
  306. SqlSessionUtil.close(sqlSession);
  307. }
  308. }

发表评论

表情:
评论列表 (有 0 条评论,46人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mybatis parameterType

    parameterType可以是基本类型,引用类型(例如:String 类型),还可以是实体类类型(POJO 类)同时也可以使用实体类的包装类 基本类型和String我们可以