zoukankan      html  css  js  c++  java
  • java开发问题汇总

    springMVC入门文章

    http://www.admin10000.com/document/6436.html

    http://www.importnew.com/15141.html

    http://www.cnblogs.com/wawlian/archive/2012/11/17/2775435.html

    http://jinnianshilongnian.iteye.com/blog/1594806

    springMVC实例

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>hello</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>
    View Code

    hello-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     
        <!-- 默认的注解映射的支持 -->
        <mvc:annotation-driven />
        <!--启用自动扫描  -->
        <context:component-scan base-package="com.zzz.demo" />
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
    View Code
    package com.zzz.demo;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
     
    @Controller
    public class HelloController {
         
        @RequestMapping(value="/hello",method=RequestMethod.GET)
        public String sayHello(Model model) {
            model.addAttribute("msg", "Hello, World!");
            return "hello";
        }
    }
    View Code
    package com.zzz.demo.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.zzz.demo.model.EmployeeVO;
    import com.zzz.demo.service.EmployeeManager;
    
    @Controller
    public class EmployeeController {
        @Autowired
        EmployeeManager em;
    
        @RequestMapping(value="/employee",method=RequestMethod.GET)
        public String getEmployee(Model model) {
            List<EmployeeVO> employees = em.getAllEmployees();
            model.addAttribute("employees", employees);
            model.addAttribute("msg", "Hello, employee!");
            return "employee";
        }
    }
    View Code
    package com.zzz.demo.dao;
    
    import java.util.List;
    
    import com.zzz.demo.model.EmployeeVO;
    
    public interface EmployeeDAO {
    
        public List<EmployeeVO> getAllEmployees();
    }
    View Code
    package com.zzz.demo.dao;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Repository;
    
    import com.zzz.demo.model.EmployeeVO;
    @Repository
    public class EmployeeDAOImpl implements EmployeeDAO {
    
        @Override
        public List<EmployeeVO> getAllEmployees() {
            List<EmployeeVO> employees = new ArrayList<EmployeeVO>();
             
            EmployeeVO vo1 = new EmployeeVO();
            vo1.setId(1);
            vo1.setFirstName("Lokesh");
            vo1.setLastName("Gupta");
            employees.add(vo1);
     
            EmployeeVO vo2 = new EmployeeVO();
            vo2.setId(2);
            vo2.setFirstName("Raj");
            vo2.setLastName("Kishore");
            employees.add(vo2);
     
            return employees;
        }
    
    }
    View Code
    package com.zzz.demo.model;
    
    import java.io.Serializable;
    
    public class EmployeeVO implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private Integer id;
        private String firstName;
        private String lastName;
     
        //Setters and Getters
     
        @Override
        public String toString() {
            return "EmployeeVO [id=" + getId() + ", firstName=" + getFirstName()
                    + ", lastName=" + getLastName() + "]";
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
    }
    View Code
    package com.zzz.demo.service;
    
    import java.util.List;
    
    import com.zzz.demo.model.EmployeeVO;
    
    public interface EmployeeManager {
    
        public List<EmployeeVO> getAllEmployees();
    }
    View Code
    package com.zzz.demo.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.zzz.demo.dao.EmployeeDAO;
    import com.zzz.demo.model.EmployeeVO;
    @Service
    public class EmployeeManagerImpl implements EmployeeManager {
    
        @Autowired
        EmployeeDAO dao;
        @Override
        public List<EmployeeVO> getAllEmployees() {
            // TODO Auto-generated method stub
            return dao.getAllEmployees();
        }
    
    }
    View Code

    hello.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    ${msg}
    </body>
    </html>
    View Code

    employee.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    ${msg}
    <br>
    <table border="1">
            <tr>
                <th>Employee Id</th>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
            <c:forEach items="${employees}" var="employee">
                <tr>
                    <td>${employee.id}</td>
                    <td>${employee.firstName}</td>
                    <td>${employee.lastName}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>
    View Code

    在Eclipse中,如何将项目中的src/main/java目录设置为源代码包?

    1. 打开navigator模式 window-> show view -> Navigator 

    2. 展开项目,打开 .classpath 文件 一般在工程 末尾

    3. 在标签 <classpath> 中 第一行添加如下 内容 <classpathentry kind="src" output="target/classes" path="src/main/java"/>

    4. 保存

    jdbcTemplate入门

    http://www.cnblogs.com/Fskjb/archive/2009/11/18/1605622.html

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
        <bean id="springDSN"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"
                value="com.mysql.jdbc.Driver">
            </property>
            <property name="url"
                value="jdbc:mysql://192.168.2.2:3306/test1?useUnicode=true&amp;characterEncoding=UTF-8">
            </property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
    
        <bean id="jdbcTemplate"
            class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
            lazy-init="false" autowire="default" dependency-check="default">
            <property name="dataSource">
                <ref bean="springDSN" />
            </property>
        </bean>
        <bean id="userDao" class="zzz.lcy.jdbcTemplate.UserDAO">
           <property name="jdbcT">
              <ref bean="jdbcTemplate" />
           </property>
        </bean>
    </beans>
    View Code
    package zzz.lcy.jdbcTemplate;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public final class SpringUtil {
    
        private static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        public static Object getBean(String beanName) {
            return ctx.getBean(beanName);
        }
    }
    View Code
    package zzz.lcy.jdbcTemplate;
    
    public class User {
    
        private Integer id;
        private String name;
        private String pwd;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }
    View Code
    package zzz.lcy.jdbcTemplate;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    
    public class UserDAO {
    
        private JdbcTemplate jdbcT;// = (JdbcTemplate)SpringUtil.getBean("jdbcTemplate");
        public void setJdbcT(JdbcTemplate jdbcT) {
            this.jdbcT = jdbcT;
        }
        public List findAll() {
            return jdbcT.queryForList("select * from user");
        }
        public List<User> findAllUsers() {
            List<User> users = new ArrayList<User>();
            List list = findAll();
            Iterator it = list.iterator();
            User user = null;
            while (it.hasNext()) {
                Map m4usr = (Map)it.next();
                user = new User();
                user.setId((Integer)m4usr.get("id"));
                user.setName((String)m4usr.get("name"));
                user.setPwd((String)m4usr.get("pwd"));
                users.add(user);
            }
            return users;
        }
        public int insert(String name, String pwd) {
            String sql = "insert into user(name,pwd) values(?,?)";
            return jdbcT.update(sql, new Object[]{name,pwd});
        }
        public int delete(int bid){
            String sql = "delete from BookInfo where bid =?";
            return jdbcT.update(sql, new Object[]{bid});
        }
        
        public static void main(String[] args) throws Exception {
            UserDAO ud = (UserDAO)SpringUtil.getBean("userDao");//new UserDAO();
    //        ud.insert("小明", "123");
            List list = ud.findAll();
            System.out.println(list.size());
            List<User> users = ud.findAllUsers();
            for (User u : users) {
                System.out.println(u.getName());
            }
        }
    }
    View Code

    java访问mysql实例

    package zzz.lcy.dbconn;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class MysqlDemo {
    
        public static void main(String[] args) throws Exception {
            Connection  conn = null;
            String sql;
            String url = "jdbc:mysql://192.168.2.2:3306/test?user=root&password=123456";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url);
            Statement stmt = conn.createStatement();
            sql = "create table student(NO char(20),name varchar(20),primary key(NO))";
            int result = stmt.executeUpdate(sql);
            if (result != -1) {
                sql = "insert into student(NO, name) values('100001','aaa'),('100002','bbb')";
                result = stmt.executeUpdate(sql);
                sql = "select * from student";
                ResultSet rs = stmt.executeQuery(sql);
                while (rs.next()) {
                    System.out.println(rs.getString(1) + "	" + rs.getString(2));
                }
                rs.close();
            }
            conn.close();
        }
    }
    View Code
  • 相关阅读:
    泛型
    hibernate--lazy(懒加载)属性
    hibernate--关联映射(一对多)
    hibernate--关联映射(多对一,一对一)
    Hibernate--基本映射标签和属性介绍
    hibernate--query接口初步
    java多线程学习--java.util.concurrent
    git 忽略文件 目录
    spring boot 扫描 其他jar包里面的 mapper xml
    windows gogs 安装
  • 原文地址:https://www.cnblogs.com/feilv/p/5586855.html
Copyright © 2011-2022 走看看