1 package com.learn.ssm.chapter3.pojo; 2 3 import java.util.Date; 4 import java.util.List; 5 6 public class Role { 7 8 private Long id; 9 private String roleName; 10 private String note; 11 private Date date; 12 private List<String> hobby; 13 14 /* 15 * public Role(Long id, String roleName, String note, Date date) { super(); 16 * this.id = id; this.roleName = roleName; this.note = note; this.date = 17 * date; } 18 */ 19 public Long getId() { 20 return id; 21 } 22 23 public void setId(Long id) { 24 this.id = id; 25 } 26 27 public String getRoleName() { 28 return roleName; 29 } 30 31 public void setRoleName(String roleName) { 32 this.roleName = roleName; 33 } 34 35 public String getNote() { 36 return note; 37 } 38 39 public void setNote(String note) { 40 this.note = note; 41 } 42 43 public Date getDate() { 44 return date; 45 } 46 47 public void setDate(Date date) { 48 this.date = date; 49 } 50 51 public List<String> getHobby() { 52 return hobby; 53 } 54 55 public void setHobby(List<String> hobby) { 56 this.hobby = hobby; 57 } 58 59 @Override 60 public String toString() { 61 StringBuilder s = new StringBuilder(); 62 63 return "id: " + id + ", roleName: " + roleName + ", note: " + note + ", date: " + date + ", hobby: " + hobby; 64 } 65 66 }
1 package com.learn.ssm.chapter3.mapper; 2 3 import java.util.List; 4 5 import com.learn.ssm.chapter3.pojo.Role; 6 7 public interface RoleMapper { 8 9 public int insertRole(Role role); 10 11 public int deleteRole(Long id); 12 13 public int updateRole(Role role); 14 15 public Role getRole(Long id); 16 17 public List<Role> findRoles(String roleName); 18 19 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.learn.ssm.chapter3.mapper.RoleMapper"> 6 7 8 9 <resultMap id="userResultMap" type="role"> 10 <result typeHandler="com.learn.ssm.chapter3.bean.MyDateTypeHandler" 11 column="date" javaType="java.util.Date" jdbcType="VARCHAR" property="date" /> 12 <result property="hobby" column="hobby" 13 typeHandler="com.learn.ssm.chapter3.bean.MyListTypeHandler" /> 14 15 <result property="roleName" column="role_name" /> 16 </resultMap> 17 18 19 20 21 22 <insert id="insertRole" parameterType="role"> 23 insert into 24 t_role(id,role_name,note,date,hobby) 25 values(#{id},#{roleName},#{note},#{date,javaType=Date,jdbcType=VARCHAR,typeHandler=com.learn.ssm.chapter3.bean.MyDateTypeHandler}, 26 #{hobby,typeHandler=com.learn.ssm.chapter3.bean.MyListTypeHandler}) 27 </insert> 28 29 <delete id="deleteRole" parameterType="Long"> 30 delete from t_role where 31 id=#{id} 32 </delete> 33 34 <update id="updateRole" parameterType="role"> 35 update t_role set 36 role_name=#{roleName},note=#{note},date=#{date} where id=#{id} 37 </update> 38 39 40 41 <select id="getRole" parameterType="Long" resultMap="userResultMap"> 42 43 select * from t_role where id=#{id} 44 45 </select> 46 47 48 49 <select id="findRoles" parameterType="string" resultType="role"> 50 select 51 id,rote_name as roleName,note from t_role where role_name like 52 concat('%',#{roleName},'%') 53 </select> 54 55 </mapper>
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 7 <properties resource="jdbc.properties"> 8 </properties> 9 10 <typeAliases> 11 <typeAlias alias="role" type="com.learn.ssm.chapter3.pojo.Role" /> 12 </typeAliases> 13 14 <environments default="development"> 15 <environment id="development"> 16 <transactionManager type="JDBC" /> 17 <dataSource type="POOLED"> 18 <property name="driver" value="${mysql_driver}" /> 19 <property name="url" value="${mysql_url}" /> 20 <property name="username" value="${mysql_username}" /> 21 <property name="password" value="${mysql_password}" /> 22 </dataSource> 23 </environment> 24 </environments> 25 26 <!-- 通过mapp标签加载配置文件 --> 27 <mappers> 28 <mapper resource="com/learn/ssm/chapter3/mapper/RoleMapper.xml" /> 29 </mappers> 30 </configuration>
1 package com.learn.ssm.chapter3.utils; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 public class SqlSessionFactoryUtils { 12 13 private final static Class<SqlSessionFactoryUtils> Lock = SqlSessionFactoryUtils.class; 14 15 private static SqlSessionFactory sqlSessionFactory = null; 16 17 public static SqlSessionFactory getSessionFactory() { 18 synchronized (Lock) { 19 if (sqlSessionFactory != null) { 20 return sqlSessionFactory; 21 } 22 23 String resource = "mybatis-config.xml"; 24 InputStream inputStream; 25 26 try { 27 inputStream = Resources.getResourceAsStream(resource); 28 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 return null; 32 } 33 34 return sqlSessionFactory; 35 36 } 37 } 38 39 public static SqlSession openSqlSession() { 40 if (sqlSessionFactory == null) { 41 getSessionFactory(); 42 } 43 return sqlSessionFactory.openSession(); 44 } 45 46 }
1 package com.learn.ssm.chapter3.bean; 2 3 import java.sql.CallableStatement; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.Date; 8 9 import org.apache.ibatis.type.BaseTypeHandler; 10 import org.apache.ibatis.type.JdbcType; 11 import org.apache.ibatis.type.MappedJdbcTypes; 12 import org.apache.ibatis.type.MappedTypes; 13 14 @MappedJdbcTypes({JdbcType.VARCHAR}) 15 @MappedTypes({Date.class}) 16 public class MyDateTypeHandler extends BaseTypeHandler<Date>{ 17 18 public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException { 19 20 //将日期格式修改为自1970年算起的毫秒数 21 preparedStatement.setString(i, String.valueOf(date.getTime())); 22 } 23 24 public Date getNullableResult(ResultSet resultSet, String s) throws SQLException { 25 return new Date(resultSet.getLong(s)); 26 } 27 28 public Date getNullableResult(ResultSet resultSet, int i) throws SQLException { 29 return new Date(resultSet.getLong(i)); 30 } 31 32 public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException { 33 return callableStatement.getDate(i); 34 } 35 36 37 }
1 package com.learn.ssm.chapter3.bean; 2 3 import java.sql.CallableStatement; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.List; 10 11 import org.apache.ibatis.type.BaseTypeHandler; 12 import org.apache.ibatis.type.JdbcType; 13 14 public class MyListTypeHandler extends BaseTypeHandler<List<String>> { 15 16 public void setNonNullParameter(PreparedStatement pre, int i, List<String> list, JdbcType jdbcType) 17 throws SQLException { 18 StringBuilder s = new StringBuilder(); 19 for (String hobby : list) { 20 s.append(hobby + ";"); 21 } 22 pre.setString(i, s.toString().substring(0, s.length() - 1)); 23 } 24 25 public List<String> getNullableResult(ResultSet r, int i) throws SQLException { 26 String s = r.getNString(i); 27 String[] stringArry = s.split(";"); 28 List<String> hobby = new ArrayList<String>(); 29 30 /* 31 * for(int j=0;j<stringArry.length;j++){ hobby.add(stringArry[j]); } 32 */ 33 return Arrays.asList(stringArry); 34 } 35 36 public List<String> getNullableResult(ResultSet r, String s) throws SQLException { 37 38 String string = r.getNString(s); 39 String[] stringArry = s.split(";"); 40 List<String> hobby = new ArrayList<String>(); 41 42 /* 43 * for(int j=0;j<stringArry.length;j++){ hobby.add(stringArry[j]); } 44 */ 45 return Arrays.asList(stringArry); 46 } 47 48 public List<String> getNullableResult(CallableStatement call, int i) throws SQLException { 49 50 String[] string = call.getNString(i).split(";"); 51 return Arrays.asList(string); 52 } 53 54 }
1 package com.learn.ssm.chapter3.main; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.log4j.Logger; 9 10 import com.learn.ssm.chapter3.mapper.RoleMapper; 11 import com.learn.ssm.chapter3.pojo.Role; 12 import com.learn.ssm.chapter3.utils.SqlSessionFactoryUtils; 13 14 public class Chapter3Main { 15 16 public static void main(String[] args) { 17 Logger log = Logger.getLogger(Chapter3Main.class); 18 19 SqlSession sqlSession = null; 20 try { 21 sqlSession = SqlSessionFactoryUtils.openSqlSession(); 22 RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class); 23 24 // 增 25 26 /* 27 * Date date=new Date(); Role r=new Role(); r.setId(11L); 28 * r.setRoleName("AAA"); r.setNote("AAA"); r.setDate(date); 29 * List<String> hobby=new ArrayList<String>(); hobby.add("唱歌"); 30 * hobby.add("跳舞"); hobby.add("滑雪"); hobby.add("读书"); 31 * r.setHobby(hobby); roleMapper.insertRole(r); sqlSession.commit(); 32 */ 33 34 // 删 35 /* 36 * roleMapper.deleteRole(6L); sqlSession.commit(); 37 */ 38 39 // 改 40 /* 41 * Date date=new Date(); roleMapper.updateRole(new 42 * Role(1L,"LiMing","LiMing",date)); sqlSession.commit(); 43 * 44 */ 45 46 // 查 47 48 Role role = roleMapper.getRole(10L); 49 // log.info(role.getRoleName()); 50 System.out.println(role); 51 sqlSession.commit(); 52 53 } finally { 54 if (sqlSession != null) { 55 sqlSession.close(); 56 } 57 } 58 59 } 60 61 }
运行结果:
注:
如果在resultMap中不进行字段和属性名的映射:
则打印出的结果中,POJO中的属性和数据库中栏位名称不同的元素会显示null
例,POJO中属性roleName
数据库中的栏位 role_name
按道理来说应该对应,但是打印出的结果为:
id: 10, roleName: null, note: AAA, date: Sun Apr 25 17:46:49 GMT+08:00 2021, hobby: {hobby}
你可以将两者设为一样,比如都设置成role_name或者roleName,这样也会打印出想要的结果,但是数据库和java中的命名规则不同,所以还是在配置中纪进行一下映射为好