mybatis+javaweb+mysql

参考博客: http://blog.csdn.net/techbirds_bao/article/details/9233599/

mysql账号是root 忘记密码是可以通过修改配置文件 然后开什么supershell改的 123456


下面这个是测试能跑通的主截图:


mybatis+javaweb+mysql_第1张图片

贴一下其他代码:

package com.yihaomen.mybatis.model;


public class User {
    
    private int id;
    private String userName;
    private String userAge;
    private String userAddress;
    
    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 getUserAge() {
        return userAge;
    }
    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }
    public String getUserAddress() {
        return userAddress;
    }
    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }


}


User.xml代码:


"http://mybatis.org/dtd/mybatis-3-mapper.dtd">



   


Config.xml代码:


"http://mybatis.org/dtd/mybatis-3-config.dtd">

     
         
   
 


   
       
       
           
           
           
             
                 
           

       

   

    
   
       
   


Test.java代码:

package com.yihaomen.test;


import java.io.Reader;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import com.yihaomen.mybatis.model.User;


public class Test {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader; 


    static{
        try{
            reader    = Resources.getResourceAsReader("Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }
    public void a(){
    System.out.println("testtest");
    }
    public static void main(String[] args) {


    SqlSession session = sqlSessionFactory.openSession();
        try {
        User user = (User) session.selectOne("com.yihaomen.mybatis.models.UserMapper.selectUserByID", 1);
        System.out.println(user.getUserAddress());
        System.out.println(user.getUserName());
        } finally {
        session.close();
        System.out.println("endddd");
        }


    }
}



index.jsp代码:

<%@page import="com.sun.mail.handlers.text_html"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import= "com.yihaomen.mybatis.model.*"%>
<%@ page import= "com.yihaomen.test.*"%>




Insert title here


asdfasdf
<%
String[] args ;
 Test te=new Test();
 te.a();
 Test.main(null); %>


你可能感兴趣的:(JAVA)