1. 导入相关jar包

ant-1.9.6.jar
ant-launcher-1.9.6.jar
aopalliance.jar
asm-5.1.jar
asm-5.2.jar
aspectj-weaver.jar
cglib-3.2.4.jar
commons-logging-1.2.jar
javassist-3.21.0-GA.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mybatis-3.4.2.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.40-bin.jar
ognl-3.1.12.jar
slf4j-api-1.7.22.jar
slf4j-log4j12-1.7.22.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.2.8.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-context-support-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-jdbc-4.2.8.RELEASE.jar
spring-orm-4.2.8.RELEASE.jar
spring-tx-4.2.8.RELEASE.jar
spring-web-4.3.6.RELEASE.jar
spring-webmvc-4.3.6.RELEASE.jar
taglibs-standard-compat-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
taglibs-standard-spec-1.2.5.jar

2. mybatis.xml

<?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>
<mappers>
<!-- 实体类映射文件 -->
<mapper resource="com/bsss/entity/userloginMapper.xml"/>
</mappers>
</configuration>

3. 实体类映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bsss.entity.userloginMapper"> <resultMap type="com.bsss.entity.Userlogin" id="UserLoginResult">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="username" jdbcType="VARCHAR" property="username"/>
<result column="userpassword" jdbcType="VARCHAR" property="userpassword"/>
<result column="usertype" jdbcType="VARCHAR" property="usertype"/>
</resultMap>
<select id="getUserlogin" parameterType="com.bsss.entity.Userlogin" resultMap="UserLoginResult">
select id,username,userpassword,usertype from userlogin where 1 = 1
<if test="username != null and !&quot;&quot;.equals(username)">and username=#{username}</if>
<if test="userpassword != null and !&quot;&quot;.equals(userpassword)">and userpassword=#{userpassword}</if>
</select>
</mapper>

4. bean.xml(spring)

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/bsss"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean> <!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:com/bsss/conf/mybatis.xml"></property>
</bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean> <bean id="userLoginDAO" class="com.bsss.dao.impl.UserloginDAOImpl">
<!-- property name必须与class中SqlSessionTemplate的变量名字保持一致 -->
<property name="sqlSession" ref="sqlSessionTemplate"></property>
</bean>
</beans>

5. 实体类

public class Userlogin {
private int id;
private String username;
private String userpassword;
private int usertype; public Userlogin() {
super();
} public Userlogin(int id, String username, String userpassword, int usertype) {
super();
this.id = id;
this.username = username;
this.userpassword = userpassword;
this.usertype = usertype;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getUserpassword() {
return userpassword;
} public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
} public int getUsertype() {
return usertype;
} public void setUsertype(int usertype) {
this.usertype = usertype;
} @Override
public String toString() {
return "Userlogin [id=" + id + ", username=" + username + ", userpassword=" + userpassword + ", usertype="
+ usertype + "]";
}
}

6. DAO接口及实现类

//接口
public interface UserloginDAO {
public Userlogin getUserloginInfo(String username,String userpassword);
} //实现类
public class UserloginDAOImpl implements UserloginDAO{
private SqlSessionTemplate sqlSession;//变量名字必须与spring bean中的property name保持一致
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
} @Override
public Userlogin getUserloginInfo(String username,String userpassword){
Userlogin ul = new Userlogin();
ul.setUsername(username);
ul.setUserpassword(userpassword);
ul = sqlSession.selectOne("com.bsss.entity.userloginMapper.getUserlogin", ul);
return ul;
}
}

7. 测试类

public class Test {

    public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("com/bsss/conf/beans.xml");//spring-bean配置文件路径
UserloginDAO uld = (UserloginDAO)ac.getBean("userLoginDAO");
Userlogin ul = uld.getUserloginInfo("admin", "admin");
System.out.println(ul);
} }

8. 执行结果

Userlogin [id=1, username=admin, userpassword=admin, usertype=0]

最新文章

  1. 低功耗蓝牙BLE之连接事件、连接参数和更新方法
  2. 《构建之法》8&amp;16
  3. 浏览器与HTML5的相辅相成
  4. 分配和释放 BSTR 的内存
  5. php sleep()的实时输出打印,清除ob缓冲区
  6. urllib.urlretrieve的用法
  7. 10款免费而优秀的图表JS插件
  8. 【web前端面试题整理01】各位加班累了吧,来做点前端面试题吧
  9. java利用JFreeChart实现各种数据统计图(柱形图,饼图,折线图)
  10. 分享一个MVC的多层架构,欢迎大家拍砖斧正
  11. jquery动态插入行
  12. 今天研究了下webservice 终于OK了
  13. BZOJ 2442: [Usaco2011 Open]修剪草坪( dp )
  14. Android Monkey自己主动化測试
  15. Asterisk 未来之路3.0_0004
  16. NodeMCU之旅(一):构建、刷入固件,上传代码
  17. Qt中不同类型数据之间的相互转换
  18. [bzoj2574] [Poi1999]Store-Keeper
  19. postman导入csv文件,批量运行
  20. tomcat配置接口访问时间

热门文章

  1. Repbase library|divergence rate|self-sequence alignment|genomic rearrangement|cutoffs|breakpoint
  2. dhtmlTree简单实例以及基本参数设置
  3. xheditor的参数配置详解
  4. Bootstrap历练实例:带有下拉菜单的标签和胶囊导航
  5. shell脚本,计算1+3+5....100等于多少?
  6. Linux配置使用SSH Key登录并禁用root密码登录(替换同理)
  7. iOS UITextView点击事件处理
  8. Solr5.0.0 DIH之增量索引
  9. int long long 的范围
  10. Spring,Mybatis,Springmvc框架整合项目(第二部分)