创建bean或属性以连接SQL Server

时间:2019-07-08 07:02:41

标签: sql-server spring maven

我是Springmaven的新手。 我有一个SQL Server,它有一个数据库和一个表。 目的是读取并显示数据。 我遵循多个链接来执行此操作,最后是我的项目结构:enter image description here。 这是我的课程和配置:

WelcomeController:

  @Controller
public class WelcomeController {

    @Autowired
    private FancoilStatisticsDao fancoilStatisticsDao;

    @RequestMapping("/welcome")
    public ModelAndView displayDetail(ModelAndView model) throws IOException{ 
        FancoilStatistics fancoilStatistics=fancoilStatisticsDao.displayList();
        ModelAndView mav = new ModelAndView("welcome");
        mav.addObject("fancoilStatistics", fancoilStatistics);
        return mav;
    }
}

FancoilStatistics:

 public class FancoilStatistics {
        private String dateOfQt;
        private String qtyPlan;
        private String qtActual;
        private String qtChecked;

        public String getDateOfQt() {
            return dateOfQt;
        }
        public String getQtyPlan() {
            return qtyPlan;
        }
        public String getQtActual() {
            return qtActual;
        }
        public String getQtChecked() {
            return qtChecked;
        }
        public void setDateOfQt(String dateOfQt) {
            this.dateOfQt = dateOfQt;
        }
        public void setQtyPlan(String qtyPlan) {
            this.qtyPlan = qtyPlan;
        }
        public void setQtActual(String qtActual) {
            this.qtActual = qtActual;
        }
        public void setQtChecked(String qtChecked) {
            this.qtChecked = qtChecked;
        }
    }

FancoilStatisticsDao:

public interface FancoilStatisticsDao {
    public FancoilStatistics displayList();
}

FancoilStatisticsDaoImp:

public class FancoilStatisticsDaoImp implements FancoilStatisticsDao {

      @Autowired
      DataSource datasource;
      @Autowired

      JdbcTemplate jdbcTemplate;
    @Override
    public FancoilStatistics displayList() {
        // TODO Auto-generated method stub
        String sql = "select top(1) * from ArvandMIS.dbo.Pln_Fc_Monitor order by Tarikh desc;" ;
        List<FancoilStatistics> fancoilStatistics = jdbcTemplate.query(sql, new FancoilStatisticsMapper());
        return fancoilStatistics.size() > 0 ? fancoilStatistics.get(0) : null;

        }
    }
    class FancoilStatisticsMapper implements RowMapper<FancoilStatistics> { 
        public FancoilStatistics mapRow(ResultSet rs, int arg1) throws SQLException {
          FancoilStatistics fancoilStatistics = new FancoilStatistics();
          fancoilStatistics.setDateOfQt(rs.getString("Tarikh"));
          fancoilStatistics.setQtyPlan(rs.getInt("QtyPlan")+"");
          fancoilStatistics.setQtActual(rs.getInt("QtyActual")+"");
          fancoilStatistics.setQtActual(rs.getInt("QtyChecked")+"");
          return fancoilStatistics;
            }
    }

-servlet文件:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:annotation-driven />
    <context:component-scan
        base-package="ir.azaroon.main.java" />
    <mvc:default-servlet-handler />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
     <mvc:resources mapping="/resources/**" location="/resources/"  
    cache-period="31556926"/>   
</beans>

web.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>Arvand</display-name>
  <welcome-file-list>

    <welcome-file>/WEB-INF/jsp/welcome.jsp</welcome-file>

  </welcome-file-list>
          <servlet>
        <servlet-name>Arvand</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Arvand</servlet-name>
        <url-pattern>/welcome.jsp</url-pattern>
        <url-pattern>/index.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>*.html</url-pattern>        
    </servlet-mapping>    
</web-app>

pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Arvand</groupId>
  <artifactId>Arvand</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>  
    <dependencies>  
    <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>4.0.0.RELEASE</version>
</dependency>   
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
        <dependency>
         <groupId>com.microsoft.sqlserver</groupId>
         <artifactId>sqljdbc4</artifactId>
         <version>4.0</version>
        </dependency>
    </dependencies>
</project>

现在我的问题是我做对了吗? 并且where and how应该创建连接详细信息吗?如用户名,网址,密码等。 我已经在Internet上看到一些帖子,它们告诉在资源目录中创建bean文件,但是我没有该目录。 也有人说创建.properties文件。 根据我的结构,我该怎么做? 请注意,我不想使用JPA

0 个答案:

没有答案