此文已由作者赵计刚授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
AdminDao
1 package com.xxx.dao.userManagement; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Repository; 7 8 import com.xxx.mapper.userManagement.AdminMapper; 9 import com.xxx.model.userManagement.Admin; 10 11 /** 12 * 管理员DAO 13 */ 14 @Repository 15 public class AdminDao { 16 @Autowired 17 private AdminMapper adminMapper; 18 19 public boolean register(Admin admin){ 20 return adminMapper.insertAdmin(admin)==1?true:false; 21 } 22 23 public Admin login(String username ,String password){ 24 return adminMapper.selectAdmin(username, password); 25 } 26 27 public List<Admin> findAdmin(String username, String password, int start, int limit){ 28 return adminMapper.getAdminByConditions(username, password, start, limit); 29 } 30 31 public int insertAdminWithBackId(Admin admin){ 32 return adminMapper.insertAdminWithBackId(admin); 33 } 34 }
AdminMapper
1 package com.xxx.mapper.userManagement; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Insert; 6 import org.apache.ibatis.annotations.Param; 7 import org.apache.ibatis.annotations.Result; 8 import org.apache.ibatis.annotations.Results; 9 import org.apache.ibatis.annotations.Select; 10 11 import com.xxx.model.userManagement.Admin; 12 13 /** 14 * 管理员Mapper 15 */ 16 public interface AdminMapper { 17 18 /**************注解**************/ 19 @Insert("INSERT INTO userinfo(username, password) VALUES(#{username},#{password})") 20 public int insertAdmin(Admin admin); 21 22 @Select("SELECT * FROM userinfo WHERE username = #{username} AND password = #{password}") 23 @Results(value = { 24 @Result(id = true, column = "id", property = "id"), 25 @Result(column = "username", property = "username"), 26 @Result(column = "password", property = "password") }) 27 public Admin selectAdmin(@Param("username") String username, 28 @Param("password") String password); 29 30 /***************xml**************/ 31 /** 32 * 条件不定式查询 33 * 我们这里使用@Param指定参数,这样的话,在AdminMapper.xml中就不用再使用parameterType属性了;否则得写parameterType属性 34 */ 35 public List<Admin> getAdminByConditions(@Param("username")String username, 36 @Param("password")String password, 37 @Param("start")int start, 38 @Param("limit")int limit); 39 40 /** 41 * 返回主键 42 */ 43 public int insertAdminWithBackId(Admin admin); 44 }
注意:在用xml传参的时候,
接口定义好之后,需要添加两个配置文件+修改两个配置文件。目录结构如下:
AdminMapper.xml(该xml的名字最好与对应接口的接口名完全相同)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 4 <!-- 指定工作空间,要与接口名相同,源代码没有去看,猜测应该是通过"这里的namespace.下边方法的id"来定位方法的 --> 5 <mapper namespace="com.xxx.mapper.userManagement.AdminMapper"> 6 <!-- 指定字段映射 --> 7 <resultMap type="Admin" id="adminResultMap"> 8 <id property="id" column="id" jdbcType="INTEGER" /> 9 <result property="username" column="username" jdbcType="VARCHAR" /> 10 <result property="password" column="password" jdbcType="VARCHAR" /> 11 </resultMap> 12 13 <select id="getAdminByConditions" resultMap="adminResultMap"><!-- 返回结果为上边指定的adminResultMap --> 14 <![CDATA[ SELECT * FROM userinfo WHERE 1=1 ]]> 15 <if test="username != null"><![CDATA[ AND username = #{username} ]]></if> 16 <if test="password != null"><![CDATA[ AND password = #{password} ]]></if> 17 <![CDATA[ ORDER BY id ASC LIMIT #{start}, #{limit} ]]> 18 </select> 19 20 <!-- 若不需要自动返回主键,将useGeneratedKeys="true" keyProperty="id"去掉即可 --> 21 <insert id="insertAdminWithBackId" parameterType="Admin" useGeneratedKeys="true" keyProperty="id"> 22 <![CDATA[ 23 INSERT INTO userinfo 24 ( 25 username, 26 password 27 ) 28 VALUES 29 ( 30 #{username, jdbcType=VARCHAR}, 31 #{password, jdbcType=VARCHAR} 32 ) 33 ]]> 34 </insert> 35 36 </mapper>
注意:
mybatis.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 6 <configuration> 7 <properties> 8 <property name="dialect" value="mysql" /> 9 </properties> 10 11 <typeAliases> 12 <!-- 这样会将com.xxx.model包及其子包下的所有类起别名为相应的简单类名 --> 13 <package name="com.xxx.model"/> 14 <!-- 如果这样去起别名的话,每一个模型类都要写一个,就比较麻烦 --> 15 <!-- <typeAlias alias="Admin" type="com.xxx.model.userManagement.Admin"/> --> 16 </typeAliases> 17 </configuration>
注意:这个文件一般用于指定属性和别名。
spring-data.xml
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" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 8 9 <!-- 注解扫描 --> 10 <context:component-scan base-package="com.xxx" /> 11 12 <!-- 引入数据源,这里变量的读取都是从ssmm0的pom.xml中读取的 --> 13 <bean id="xxxDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 14 <property name="driverClassName" value="${jdbc.driverClassName}" /> 15 <property name="url" value="${jdbc.url}" /> 16 <property name="username" value="${jdbc.username}" /> 17 <property name="password" value="${jdbc.password}" /> 18 </bean> 19 20 <!-- 引入mybatis --> 21 <bean id="xxxSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 22 <property name="dataSource" ref="xxxDataSource" /> 23 <!-- 以下两个属性是专门为xml方式配置的,若只使用注解方式,这两个属性行可以不配置 --> 24 <property name="configLocation" value="classpath:mybatis.xml"/> 25 <property name="mapperLocations"> 26 <list> 27 <value>classpath*:mapper/admin/*Mapper.xml</value> 28 </list> 29 </property> 30 </bean> 31 <bean id="xxxMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 32 <!-- 33 这里就是包名为什么就做com.xxx.mapper.user而非com.xxx.user.mapper, 34 这样的话,比如说有两个项目com.xxx.mapper.user和com.xxx.mapper.hotel,value只需写作com.xxx.mapper即可 35 否则,value就要写作com.xxx.user.mapper,com.xxx.hotel.mapper 36 --> 37 <property name="basePackage" value="com.xxx.mapper" /> 38 <property name="sqlSessionFactoryBeanName" value="xxxSqlSessionFactory" /> 39 </bean> 40 41 </beans>
说明:只增加了两个属性配置,看代码与注释。
注:关于classpath与classpath*的具体区别自己去查,简单来说就是两句话:classpath只加载第一个找到文件;classpth*加载找到的多个文件。
免费领取验证码、内容安全、短信发送、直播点播体验包及云服务器等套餐
更多网易技术、产品、运营经验分享请点击。