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

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


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


mybatis+javaweb+mysql

贴一下其他代码:

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代码:

<?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.yihaomen.mybatis.models.UserMapper">
    <select id="selectUserByID" parameterType="int" resultType="User">
        select * from `user` where id = #{id}
    </select>
</mapper>


Config.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>
    <typeAliases> 
        <typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/> 
    </typeAliases> 


    <environments default="development">
        <environment id="development">
        <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://127.0.0.1:3306/medicine" />
            <property name="username" value="root" />  
                <property name="password" value="123456" />  
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/yihaomen/mybatis/model/User.xml"/>
    </mappers>
</configuration>

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.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
asdfasdf
<%
String[] args ;
 Test te=new Test();
 te.a();
 Test.main(null); %>
</body>
</html>

相关文章:

  • 2021-06-21
  • 2021-06-21
  • 2021-06-08
  • 2021-05-08
  • 2022-12-23
  • 2021-03-30
  • 2021-05-18
  • 2021-04-15
猜你喜欢
  • 2022-03-04
  • 2021-12-12
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-12-30
相关资源
相似解决方案